Import AppleScript methods in another AppleScript?

后端 未结 4 1032
萌比男神i
萌比男神i 2020-12-29 05:24

Is there a way to use defined AppleScript methods in other AppleScripts which reference the original AppleScript with something similar to import (f.e. in PHP)?

I wr

相关标签:
4条回答
  • 2020-12-29 05:44

    You can follow this repo https://github.com/abbeycode/AppleScripts which organises its scripts into libraries

    Here is an example https://github.com/abbeycode/AppleScripts/blob/master/Scripts/Download%20Chase%20Activity.applescript

    property LibLoader : load script file ((path to scripts folder from user domain as text) & "Libraries:Library Loader.scpt")
    property TransmissionLib : LibLoader's loadScript("Libraries:Transmission.applescript")
    property GrowlLib : LibLoader's loadScript("Libraries:Growl.applescript")
    property SafariLib : LibLoader's loadScript("Libraries:Safari.applescript")
    property DatesLib : LibLoader's loadScript("Libraries:Dates.applescript")
    
    0 讨论(0)
  • 2020-12-29 05:45

    One way to import another script as a library is to define a property which is initialized by loading the library as a script object. You can then use the tell command to invoke the library functions.

    property pSkypeLibrary : load script POSIX file "/Users/sakra/Desktop/skype_methods.scpt"
    
    tell pSkypeLibrary
        setSkypeStatus("status", "mood")
    end tell
    
    0 讨论(0)
  • 2020-12-29 05:46

    There is a more elegant way of doing this. You can save your skype_methods.scpt file inside a Script Libraries folder on your computer.

    Depending on how you want to define the availability of this library, you use a different folder.

    • Place the skype_methods.scpt file inside the /Library/Script Libraries folder to make it available for all users on the computer.
    • Place it in the ~/Library/Script Libraries folder to make it available for a specific user only.

    You can then use all the handlers in that library as follows:

    property Skype : script "skype_methods"
    Skype's setSkypeStatus("status","mood")
    

    This prevents the need of numerous tell blocks when using handlers from different libraries.

    0 讨论(0)
  • 2020-12-29 06:02

    Script Foo.scpt

    set theBar to "path:to:Bar.scpt" as alias
    run script (theBar)
    

    Script Bar.scpt

    display dialog "Bar"
    

    Result: A window that displays "Bar"

    0 讨论(0)
提交回复
热议问题