AppleScript:当前应用程序的路径

时间:2015-04-15 16:04:39

标签: macos applescript

快速提问。我正在尝试使用AppleScript(Cocoa AppleScript应用程序)获取当前应用程序的路径。基本上,我正在尝试确定应用程序的位置,以便我可以使用应用程序操作磁盘映像中文件夹内的文件。我想知道是否有更直接的方式返回路径,我目前有:

set Var1 to (path to current application as text) & "Resources:File.xml"

返回:“Macintosh HD:Applications:Utilities:Script Editor.app:Resources:File.xml”

没关系,除了我想在路径中省略应用程序的名称。我知道我可以通过一些文本操作来做到这一点,但我必须假设除了将其切割成零件之外还有一种更清洁的方法。谢谢!

2 个答案:

答案 0 :(得分:1)

在脚本编辑器中尝试

tell application "Finder"
set current_path0 to container of (path to me) as alias
end tell
set current_path1 to quoted form of (POSIX path of current_path0)

这应该使类似的东西返回

"'/Users/YourUsername/Library/Autosave Information/'"

存储为变量的

'/Users/YourUsername/Library/Autosave Information/'

如果您也想花哨的话,请尝试这个

tell application "Finder"
set current_path0 to container of (path to me) as alias
end tell
set current_path1 to quoted form of (POSIX path of current_path0)
tell application "System Events" 
display alert current_path1
end tell

这将导致一个带有信息作为变量格式的警报框,看起来像这样

'/Users/YourUsername/Library/Autosave Information/'

获取此脚本所在的应用程序的目录,并将其另存为应用程序,并将其存储在将找到该应用程序所在根的任何位置,然后您只需手动输入应用程序名称即可,我使用了另一个脚本来存储该应用程序名称在应用程序的“资源”文件夹中调用文件

    tell application "Finder"
set current_path0 to container of (path to me) as alias
end tell
set current_path1 to quoted form of (POSIX path of current_path0)
tell application "System Events" 
display alert current_path1
end tell
set current_path2 to text 1 thru -2 of current_path1
set appName to "Example.app'"
set AppFromRoot to current_path2 & appName

返回与此类似的内容

"'/Users/YourUsername/Library/Autosave Information/Example.app'"

存储为变量的

看起来像这样

'/Users/YourUsername/Library/Autosave Information/Example.app'

您必须在该文件的根目录中将变量AppFromRoot定义为您要进入的应用程序名称,或者可以在其他脚本应用程序中使用此脚本将其设为变量,而这取决于您使用它的方式为

答案 1 :(得分:0)

简而言之,不可能:(抱歉打破你的泡泡。如果你看一下应用程序的词典,你会看到它继承自应用程序。

启动Apple Script编辑器并查看:

File > Open Dictionary > Whatever Your App Is Called

所以你将不得不处理基本操作。