当前位置: 首页 > 知识库问答 >
问题:

使用参数将AppleScript编译成应用程序

荀嘉熙
2023-03-14

有一种AppleScript方法:

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

我想编译这个脚本,并将参数传递到(而不是用osascript运行它!)我的申请书。应用程序

类似的东西

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

在这种情况下,我将有编译的应用程序,我可以运行。寻找一个关于如何使用传递参数精确编译脚本的命令。因为编译需要很多时间——我只想做一个。运行后My_Application.app比通过osascript更快。如果输入参数改变了——只需重新编译应用程序。

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

共有1个答案

后学
2023-03-14

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

进程信息参数包括可执行路径,但可以跳过。以这种方式获取参数也适用于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应用程序,可以在源文件中使用占位符文本,然后使用脚本或文本编辑器替换它。然后可以使用OSAComile 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",上面),然后输出一个编辑过的源文件和一个由它构建的应用程序到您的桌面上。

 类似资料:
  • 最近开始使用react原生和project要求在服务器上构建应用程序。所以理论是,应用程序可以根据请求构建,这意味着一些东西,让我们称之为react原生编译器,需要在某个服务器上,允许我这样做。 例如,这是react native compiler位于“http://example.com/compile”的位置,您在该站点上有一些设置选项和按钮“Compile”,当您单击按钮时,应用程序编译器启

  • 我正在开发一个很久以前创建的应用程序(可能是2008/2009)。现在我得到了升级服务器的任务。以前它是在jdk1.4中编译的。现在我必须使应用程序与jdk2.7兼容。我试图用jre7编译它,并且我已经更改了ant build的源代码和目标值。xml文件到。而且在ANT构建中,jre7被选为编译器环境。我收到以下错误消息: 我尝试使用以下行打印java版本: 它显示: [echo]使用Java版本

  • 问题内容: 我已经使用构建了一个简单的可执行程序。 我已经将代码编译成静态二进制程序。 我想反编译输出二进制文件并获取Go源代码。 这有可能吗? 问题答案: 没有工具可以执行此操作,并且由于Go程序已编译为机器代码,因此它们所包含的信息不足,无法将其转换回Go代码。但是,仍然可以使用标准拆卸​​技术。

  • 问题内容: 我正在使用pm2启动我的应用程序,但无法将参数传递给它。我正在使用的命令是pm2 start app.js-dev。尽管这永远有效。 问题答案: 您可以按照此票证所述进行操作:https : //github.com/Unitech/pm2/issues/13 尽管如果通过环境,则可能需要考虑利用环境变量。使用此工具,您可以创建一个变量,该环境中的任何进程都可以访问该变量。 因此,您有

  • 我的项目中有这个错误。但是,找不到任何地方如何解决它。有人有这个问题吗? 它会导致每次都重新运行构建。 使用静态编程语言编译守护程序不成功 无法连接到kotlin守护程序。使用回退策略。

  • 问题内容: 我试图安装有,但我在损失的如何文件需要被写入。 当安装过程之后安装完成且没有错误,但没有关于增加从1(由环境变量OMP_NUM_THREADS控制)使用OpenBLAS线程数性能下降。 我不确定OpenBLAS集成是否完美。任何人都可以提供文件来实现相同目的。 PS:OpenBLAS与基于Python的Theano等其他工具包的集成,可在同一台计算机上通过增加线程数显着提高性能。 问题