如何查找正在执行的AppleScript的文件名

如何查找正在执行的AppleScript的文件名,applescript,Applescript,如何查找正在执行的AppleScript的名称 原因:我想创建一个脚本,根据其文件名更改其行为。比如: if myname is "Joe" then ACTION1 else if myname is "Frank" then ACTION2 else ACTION3 获取名称的正常方法是使用“我的名字”。但是,applescripts是由AppleScriptRunner运行的,所以当您在脚本上使用它时,您会得到“AppleScriptRunner”作为名称。如果您将脚本编译为应用程序,那

如何查找正在执行的AppleScript的名称

原因:我想创建一个脚本,根据其文件名更改其行为。比如:

if myname is "Joe" then ACTION1
else if myname is "Frank" then ACTION2
else ACTION3

获取名称的正常方法是使用“我的名字”。但是,applescripts是由AppleScriptRunner运行的,所以当您在脚本上使用它时,您会得到“AppleScriptRunner”作为名称。如果您将脚本编译为应用程序,那么“我的名字”将起作用。获取脚本名称的唯一方法是获取其路径并提取名称。这样的东西会对脚本起作用

getMyName()
tell me to display dialog result

on getMyName()
    set myPath to path to me as text
    if myPath ends with ":" then
        set n to -2
    else
        set n to -1
    end if
    set AppleScript's text item delimiters to ":"
    set myName to text item n of myPath
    if (myName contains ".") then
        set AppleScript's text item delimiters to "."
        set myName to text 1 thru text item -2 of myName
    end if
    set AppleScript's text item delimiters to ""
    return myName
end getMyName

查找路径基本部分的一种更简单的方法是使用的
名称

tell application "Finder"
    set p to path to me
    set nam to name of file p as text
end tell

以下是一种方法,该方法适用于以下所有情况:

  • *.scpt
    文件(编译的AppleScript文件;在AppleScript编辑器中运行或使用
    osascript
  • *.applescript
    文件(未编译的applescript文件;在applescript编辑器中运行或使用
    osascript
  • 直接包含AppleScript的命令行脚本(标记为可执行,以
    #!/usr/bin/env-osascript
    开头):
  • *.app
    使用AppleScript编辑器创建的文件
  • *.app
    使用Automator创建的包含AppleScript操作的文件
注:相比之下,它不适用于以下情况:

  • 使用包含AppleScript操作的Automator创建的OS X服务(特殊
    *.workflow
    文件)-始终报告“WorkflowServiceRunner[.xpc]”
  • 通用
    *。工作流
    使用Automator创建的文件,其中包含AppleScript操作,并使用
    Automator运行
    -始终报告“Automator Runner[.app]”
获取正在运行的脚本的名称,包括文件扩展名(
.scpt
.app
.applescript
,视情况而定):

如果要使用单个命令删除文件扩展名,请使用以下基于
do shell script
的方法:

tell application "System Events" to set myname to do shell script "rawName=" & quoted form of (get name of (path to me)) & "; printf '%s' \"${rawName%.*}\""
这里有一个更详细(但根据AppleScript标准更简洁)的全AppleScript替代方案:

最后,这里有一个变体:可以使用
将myname设置为getMyName()
调用的子例程:

也许是这样:
将appname设置为当前应用程序的名称

如果名称包含单引号或反斜杠,则shell脚本不起作用。您可以使用
的引号形式
对字符串进行转义,并将
shopt-u xpg_echo
添加到echo之前。@LauriRanta说得好,谢谢;我已经更新了帖子。(我原以为单引号就足够了,但正如你所指出的那样,事实并非如此。虽然某些反斜杠是可以的,但与以下字符一起形成转义序列的反斜杠会导致问题。请注意,理论上,使用
echo-E
而不是
shopt-u xpg\u echo
,应该足够了,但这是一种莫名其妙的做法。)使用
do shell script
运行时会出现错误。默认情况下,echo在sh(或POSIX模式)中没有-E选项。谢谢,很高兴知道-我没有意识到
do shell script
在POSIX兼容模式下运行所有命令。您也可以使用
printf%s
<代码>shopt-uo posix;echo'\t'仍然打印文本选项卡,除非添加
-E
。do shell脚本的默认shell是
/bin/sh
,这是一个。指向我的
路径
信息非常好,但是请注意,使用(指向我的路径的信息)的
名称
可以更容易地访问文件名。(
info for
也有一个报告文件扩展名的属性,但AppleScript缺少字符串替换命令意味着在这种情况下没有帮助。)如果您想用一个命令删除扩展名,请参阅我的
do shell script
方法。我停止使用“info for”命令几个版本的applescript。苹果不赞成这个命令。当然,“info for”仍然可用,但在某个时候if将消失,因此如果您使用它,请注意这一点。感谢您让我知道-我不知道
info for
已被弃用-最好避免使用它,然后。您可以按如下方式使用它的替换:
告诉应用程序“系统事件”设置myname以获取名称(指向我的路径)
。我已相应地更新了我的答案。
tell application "System Events" to set myname to do shell script "rawName=" & quoted form of (get name of (path to me)) & "; printf '%s' \"${rawName%.*}\""
tell application "System Events"
    set myname to name of (path to me)
    set extension to name extension of (path to me)
end tell
if length of extension > 0 then
    # Make sure that `text item delimiters` has its default value here.
    set myname to items 1 through -(2 + (length of extension)) of myname as text
end if
on getMyName()
    local myName, tidSaved
    tell application "System Events"
        set myAlias to path to me -- alias to the file/bundle of the running script
        set myName to name of myAlias -- filename with extension, if any.
        if name extension of myAlias is not "" then -- strip away extension
            set {tidSaved, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {""}}
            set myName to items 1 through -(2 + (length of (get name extension of myAlias))) of myName as text
            set AppleScript's text item delimiters to tidSaved
        end if
    end tell
    return myName
end getMyName