0

Here is my script :

property MyLabel : missing value

on buttonClicked_(sender)
    set the plistfile_path to "~/Desktop/MY_DATA.plist"
    tell application "System Events"
        set p_list to property list file (plistfile_path)
        -- read the plist data
        set theMyDataFromPlist to value of property list item "DATA1" of p_list

         end tell
end buttonClicked_

This gonna take the data I want from a plist and set in a variable (theMyDataFromPlist).

How can I print this variable on the label "MyLabel" and make the script auto refresh when the data change ?

also, when I click on the text (or another button), can I copy the value to the clipboard. (would set the clipboard to theMyDataFromPlist work?)

I also wonder is that possible to do the same with Swift?

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Kevin
  • 1,076
  • 4
  • 22
  • 49

1 Answers1

1

How can I print this variable on the label "MyLabel"

Where would you like to print the variable? You can make AppleScript display a dialog:

set variable to 42
display dialog "The value of variable is " & variable

AppleScript is not designed as a terminal language, it is for UI scripting, so it's not like most scripting languages that just offer a print as where would the user see the printed value.

and make the script auto refresh when the data change ?

Not quite sure what you expect here. That your system magically runs your script whenever the content of a file changes? That's not going to happen. You can create a launchd job and have launchd monitor that file and then execute your script when the file changes; this is described here:

https://discussions.apple.com/message/13182902#13182902

But some process will have to monitor the file and if your script should do so, it has to be running all the time, non-stop. Then you could make some code run once every X seconds, checking the file last modification date and whenever that changes, re-read the plist. This polling is super ugly but the best thing that AS can do out of the box.

BTW where is the rest? You say

also, when I click on the text (or another button), can I copy the value to the clipboard.

Which text? Which button? Sound like you have a whole application there but all you showed us are 11 lines script code. You didn't even mention that you have a whole application with a UI. Your question starts with "Here is my script", so you make it sound like this 11 lines is all that you have.

(would set the clipboard to theMyDataFromPlist work?)

Why don't you simply try it out? Pasting that line into ScriptEditor would have taken equally long than asking this question. I just tried it and it turns out that you can only set strings.

This code won't work:

-- bad code
set variable to 42
set the clipboard to variable

But this code does work:

-- good code
set variable to 42
set the clipboard to "" & variable

I also wonder is that possible to do the same with Swift?

Personally I would not even consider writing an application in AppleScript; I'd rather stop writing code before I do that. Of course this can be done in Swift or in Obj-C. Everything you can do in AS can be done in these other two languages and no, the opposite doesn't hold true.

Using Obj-C or Swift, you can also use GCD and with GCD monitoring a file for changes is easy. Just see

https://stackoverflow.com/a/11447826/15809

Community
  • 1
  • 1
Mecki
  • 125,244
  • 33
  • 244
  • 253
  • Thank for the reply, yes but I'm getting data from webpage, (document.getElementsByClassName) which can't be done by swift or at least that what I was told. also I still don't manage to set my data to the clipboard, probably from something really silly in my code – Kevin Feb 10 '17 at 12:49
  • @KevinCork Every C and Obj-C API can also be used in Swift; really every API, you only need to include the header files in your bridging header to make the Swift compiler aware of that API (frameworks from Apple are modules, these can be directly `import`ed into Swift code, like `import Dawin` for example. Everything that is no module will still has a header file and this file can be included into a bridging header). Make a new question, put there some actual code (not just 11 lines of unrelated AppleScript) and somebody here will show you the same code in Swift within minutes. – Mecki Feb 13 '17 at 11:35
  • @KevinCork Regarding setting the clipboard, if you don't show me your code, then I cannot help you, sorry. And I still don't know what the AppleScript Code in your question has to do with all this. It doesn't contain `document.getElementsByClassName`, there's no attempt to write to clipboard either. If you have different questions, just make different questions on SO; please don't put plenty of unrelated issues into a single question. SO is users help users and only if you ask one question at a time other users will find your question if they have a similar problem and can profit of the reply. – Mecki Feb 13 '17 at 11:40