Search code examples
xcodevariablesapplescriptplist

Xcode AppleScript : change label which content of variable


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?


Solution

  • 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