Import AppleScript methods in another AppleScript?

2019-02-03 01:55发布

问题:

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 wrote a methode to set Skype status and mood-text:

on setSkypeStatus(status, mood_text)
    tell application "System Events"
        set skypeRunning to count (every process whose name is "Skype")

        if skypeRunning > 0 then --only set status if skype is running
            tell application "Skype"
                set myStatus to "SET USERSTATUS " & status
                set myMood to "SET PROFILE MOOD_TEXT " & mood_text

                send command myStatus script name "AppleScript"
                send command myMood script name "AppleScript"
                return skypeRunning
            end tell
        else
            return skypeRunning
        end if
    end tell
end setSkypeStatus

now I'm searching for something like import skype_methods.scpt. Is there such a functionality. I can't something related with Google.

回答1:

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


回答2:

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"



回答3:

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.



回答4:

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")