Applescript子程序

时间:2016-05-16 21:51:24

标签: applescript

我有一个helperScript,它有一些我经常使用的基本功能。

My Current脚本的流程如下:

on SubA()
    Set HelperScript to load…..
    tell HelperScript
    : :
        : :
    end tell
end SubA

on SubB()
    Set HelperScript to load…..

    tell HelperScript
    ::
        ::
    end tell
end SubB

on run paravlist
    Set HelperScript to load…..

    tell HelperScript

    SubA()

    SubB()

    end tell
end run 

我无法调用SubA()和SubB(),因为正在设置辅助脚本并使用每个子例程。如果我注释掉helperScript的用法。我可以从彼此调用子程序。处理这样的问题的最佳方法是什么?我想在每个子例程中使用helperScript。

2 个答案:

答案 0 :(得分:0)

在再次阅读你的问题之后,我想我已经弄明白了你在问什么。您是否尝试在方法中加载脚本,然后要调用该脚本中的方法?

如果是这种情况,我认为您正在寻找的是......

set HelperScript to load script...
set theResult to someMethod() of HelperScript

编辑: 我还不清楚你是否有两个脚本,所以我已经更新了答案以反映这两个案例。

双脚本示例...

property HelperScript : null

on run
    try
        if not loadScript() then error "Unable to load script"

        set rslt1 to SubA() of HelperScript -- This approach assumes HelperScript.scpt is a different script and it contains a method called SubA
        set rslt2 to SubB() of HelperScript -- This approach assumes HelperScript.scpt is a different script and it contains a method called SubB

    on error errMsg
        activate
        display dialog "Error: " & errMsg buttons {"OK"} default button 1 giving up after 10
    end try
end run


on loadScript()
    try
        set HelperScript to load script (POSIX file "/Path/To/HelperScript.scpt")
        return true
    on error
        return false
    end try
end loadScript

单脚本示例...

on run
    try
        set rslt1 to SubA() -- This approach assumes your HelperScript is THIS script
        set rslt2 to SubB() -- This approach assumes your HelperScript is THIS script
    on error errMsg
        activate
        display dialog "Error: " & errMsg buttons {"OK"} default button 1 giving up after 10
    end try
end run

on SubA()
    try
        -- Do something here

        return true -- or some other value
    on error
        return false -- or some other value
    end try
end SubA

on SubB()
    try
        -- Do something here

        return true -- or some other value
    on error
        return false -- or some other value
    end try
end SubB

答案 1 :(得分:0)

自10.9以来,AppleScript已包含library loading system。它不是很好(例如,避免SDEF垃圾,因为它是1. make-work和2. bug-injector)但它通常可以完成这项任务。我建议你采用它。