将 AppleScript 编译成带参数的应用程序

Compile AppleScript into application with parameters

有一个 AppleScript 方法:

on displayError(theErrorMessage)
    display dialog theErrorMessage
    return "done"
end displayError

我想编译这个脚本并将参数传入(而不是运行它用osascript!)My_Application.app

类似于

osacompile - o My_Application.app My_Script.applescript "This is error message as parameter" 

在这种情况下,我将编译我可以运行 的应用程序。寻找有关如何使用传递参数准确编译脚本的命令。由于编译需要很多时间 - 我只想做这些。在 运行 My_Application.app 之后,什么比通过 osascript 更快。如果输入参数发生变化 - 只需重新编译应用程序。

一个不错的选择是以某种方式从 运行ning 应用程序中收集 return 值,但这是另一个问题

要获取 AppleScript 应用程序 的命令行参数,您可以通过一些 AppleScriptObjC 使用 NSProcessInfo。主要问题是没有方便的方法 return 将结果发送到命令行,因此您需要执行其他操作,例如写入文件。

进程信息参数包括可执行路径,但可以跳过。以这种方式获取参数也适用于 osascript,尽管它的路径也被添加到参数中。

以下将作为脚本或应用程序运行:

use framework "Foundation"
use scripting additions

on run
    set arguments to (current application's NSProcessInfo's processInfo's arguments) as list
    if first item of arguments contains "osascript" then set arguments to rest of arguments -- skip osascript path
    if (count arguments) is 1 then set end of arguments to "no arguments"
    repeat with anItem in rest of arguments -- skip the main executable path
        displayError(anItem)
    end repeat
    # osascript still returns the last result
end run

on displayError(theErrorMessage)
    display dialog theErrorMessage
    return "done"
end displayError

终端,您可以使用多种命令:

/path/to/application.app/Contents/MacOS/applet "this is a test" "Another test"
open /path/to/application.app --args "this is a test" "Another test"
osascript /path/to/script.scpt "this is a test" "another test"

要使用脚本参数来编译 AppleScript 应用程序,您可以在源文件中使用占位符文本,然后使用脚本或文本编辑器替换它。然后可以使用 osacompile shell 实用程序将源代码编译成应用程序。它采用文本或脚本文件,结果基于输出文件的扩展名(-o 选项)。

完整示例:

Test.applescript 文件(这将用作模板 - 占位符文本将在编辑后的输出文件中被替换):

display dialog "This is a test.
It is only a test.

The date is ##DATE##
Some name: ##NAME##
An Identifier: ##ID##

End of test."

应用程序脚本:

use framework "Foundation"
use scripting additions

global arg1 -- this will be the replacement for the NAME parameter
global arg2 -- this will be the replacement for the ID parameter

on run -- example
    try
        set arguments to (current application's NSProcessInfo's processInfo's arguments) as list
        if first item of arguments contains "osascript" then set arguments to rest of arguments
        set arguments to rest of arguments
        if (count arguments) < 2 then set arguments to getArguments()
        set {arg1, arg2} to arguments
        processFile(choose file with prompt "Choose the AppleScript source text file:" of type "com.apple.applescript.text")
    on error errmess
        display alert "Error" message errmess
    end try
end run

to getArguments()
    set theName to text returned of (display dialog "Enter 'Name' parameter:" default answer "Jane Scripter")
    set theID to text returned of (display dialog "Enter 'Identifier' parameter:" default answer "42")
    return {theName, theID}
end getArguments

to processFile(theFile) -- get a list of file items for fixPlaceholders
    set outputFile to (((path to desktop) as text) & "edited.applescript")
    set datePlaceholder to "##DATE##"
    set namePlaceholder to "##NAME##"
    set idPlaceholder to "##ID##"
    set _date to " -e \"s/" & datePlaceholder & "/`date '+%m-%d-%y'`/g\" "
    set _name to " -e \"s/" & namePlaceholder & "/" & arg1 & "/g\" "
    set _id to " -e \"s/" & idPlaceholder & "/" & arg2 & "/g\" "
    set theFile to theFile as text
    set output to (do shell script "cat " & quoted form of ((POSIX path of theFile)) & " | sed " & _date & _name & _id)
    (my output:output toFile:outputFile)
    do shell script "osacompile -o " & quoted form of POSIX path of (((path to desktop) as text) & "Finished.app") & space & quoted form of POSIX path of outputFile
end processFiles

to output:someThing toFile:someFile
    try
        set fileRef to (open for access someFile with write permission)
        set eof of fileRef to 0 -- clear any existing
        write someThing to fileRef -- overwrite
        close access fileRef
    on error errmess
        log errmess
        try -- make sure file is closed on any error
            close access fileRef
        end try
    end try
end output:toFile:

终端,上面的应用程序可以运行通过使用下面的,其中第一个参数将用于"NAME"参数和"ID" 参数的第二个:

open /path/to/application.app --args "First, Last" "Yep, looks like you."

应用程序将请求源文件("Test.applescript",上文),然后将编辑后的源文件和基于它构建的应用程序输出到您的桌面上。