有没有办法在MacOSX上用Xcode将bash shell脚本与AppleScriptObjC应用程序打包?
我正在尝试使用AppleScriptObjC作为包装器来自动化三到四个Bashshell脚本。这将为我提供一个友好的gui前端来选择数据文件等,以及方便的文本文件操作和几个bash脚本的处理 一切都正常(我可以按我的按钮并运行shell脚本),只是我不知道如何使东西半便携。我希望有一个应用程序或软件包,我可以分发给其他人,所需的脚本文件以某种方式合并或由应用程序生成,而不是向用户公开 我是Xcode和AppleScriptObjC(ASOC)的noob,我不知道该怎么做。 -我可以将脚本作为一组简单的do shell脚本命令运行,也可以通过发送到终端窗口的命令运行,但这意味着必须分发所有单独的shell文件,并将其作为一组片段放在正确的位置。 -我试图从ASOC中生成脚本文件,但由于ASOC处理字符串和特殊字符的方式,我在构建文件时遇到了障碍。我尝试过普通字符串或字符串构造的引用形式。作为背景,是相关的。以这种方式创建文件会丢失特殊字符。此代码演示了问题(例如,我做错了什么!): 如果没有办法从ASOC中创建所需的shell脚本,或者访问作为支持文件包含在应用程序包中的脚本,那么我将要做大量从bash到AppleScriptObjC的复杂移植 我们将欣然接受以下建议或解决方案: 1.如何使用AppleScriptObjC编写任意多行文件,包括控制字符等。 2.或有没有办法在MacOSX上用Xcode将bash shell脚本与AppleScriptObjC应用程序打包?,bash,escaping,applescript-objc,Bash,Escaping,Applescript Objc,我正在尝试使用AppleScriptObjC作为包装器来自动化三到四个Bashshell脚本。这将为我提供一个友好的gui前端来选择数据文件等,以及方便的文本文件操作和几个bash脚本的处理 一切都正常(我可以按我的按钮并运行shell脚本),只是我不知道如何使东西半便携。我希望有一个应用程序或软件包,我可以分发给其他人,所需的脚本文件以某种方式合并或由应用程序生成,而不是向用户公开 我是Xcode和AppleScriptObjC(ASOC)的noob,我不知道该怎么做。 -我可以将脚本作为一组
3.我如何打包一个shell脚本,以便ASOC可以在需要时运行它?经过几个小时的研究和实验,回答我自己的问题(感谢macscripter.net和autodidaktos.com上的各种帖子):
- 在项目导航器或跳转栏中选择您的项目
- 选择“构建阶段”选项卡
- 展开“复制捆绑资源”部分(这适用于Xcode 4.6.3,可能与其他版本不同)
- 通过单击列表底部的“+”按钮,将项目添加到捆绑资源中
- 选择要添加到捆绑包的脚本文件,例如yourScriptFile.sh
on applicationWillFinishLaunching_(aNotification)
set pathToResources to (current application's class "NSBundle"'s mainBundle()'s resourcePath()) as string
end applicationWillFinishLaunching_
property pathToResources : "NSString" -- works if added before script command
set yourScript to pathToResources & "/yourScriptFile.sh"
-- gives the complete unix path
-- if needed, you can convert this to the Apple style path:
set yourScriptPath to (((yourScript as text) as POSIX file) as alias)`
tell application "Finder"
open yourScriptPath
end tell
然后应该可以使用
do shell脚本
构造执行脚本或将脚本复制到某个地方供用户访问。如果合适,还可以直接从捆绑包执行脚本。这考虑了以下事实:
firstScript=指向名为scriptNumberOne.sh的脚本的变量名scriptNumberOne.sh=已嵌入应用程序以运行的脚本
按钮handlerrunscript \=在Xcode中的名称
pathToResources=指向我的应用程序的内部资源文件夹的变量,无论其当前位置如何 使用这些信息,下面是我的AppleScriptObjC Xcode项目中一个香草
AppDelegate.applescript
的副本:
script AppDelegate
property parent : class "NSObject"
property pathToResources : "NSString"
on applicationWillFinishLaunching_(aNotification)
set pathToResources to (current application's class "NSBundle"'s mainBundle()'s resourcePath()) as string
end applicationWillFinishLaunching_
on ButtonHandlerRunScript_(sender)
set firstScript to pathToResources & "/scriptNumberOne.sh"
do shell script firstScript
end ButtonHandlerRunScript_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
对于shell脚本来说可能有所不同,但是对于perl脚本来说,您可以用更少的步骤来完成这项工作 刚才是这样做的,在XCode 6中(在5中也是这样):
(资源“myAwesomeScript.pl”的路径)
- 不要忘记在.pl和脚本参数之间添加空格(
)&“&
- 最后一个“引用形式的”是它所在的位置,以便控制台日志行输出一个未经扫描的UNIX路径。这是我的偏好,为了可读性。您可能更喜欢另一种方式,即在“set theLogsFolder”行中添加引号形式的,以便日志输出可以轻松地复制/粘贴到终端命令中
script AppDelegate
property parent : class "NSObject"
property pathToResources : "NSString"
on applicationWillFinishLaunching_(aNotification)
set pathToResources to (current application's class "NSBundle"'s mainBundle()'s resourcePath()) as string
end applicationWillFinishLaunching_
on ButtonHandlerRunScript_(sender)
set firstScript to pathToResources & "/scriptNumberOne.sh"
do shell script firstScript
end ButtonHandlerRunScript_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
on doCoolStuffWithLogsFrom(theThingName)
set theLogsFolder to POSIX path of ((path to home folder as string) & "Library:Logs:Something:Something:" & theThingName)
tell me to log "Processing logs in: " & theLogsFolder
do shell script "/usr/bin/perl " & (quoted form of POSIX path of (path to resource "doCoolStuffWithLogs.pl")) & " " & quoted form of theLogsFolder
end doCoolStuffWithLogsFrom