且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何找到一个执行的AppleScript的文件名

更新时间:2022-10-23 20:40:29

正常途径获取名称是使用我的名字。但是的AppleScript由AppleScript运行运行,所以当您使用上的脚本,你得到AppleScript运行作为名称。如果您编译脚本,然后应用程序我的名字,将正常工作。拿到脚本名的唯一方法是通过获取其路径和提取的名称。这样的事情将因此脚本的工作...

  getMyName()
告诉我,以显示对话框结果在getMyName()
    设置为mypath中路径,我作为文本
    如果mypath中结尾:再
        将n设置为-2
    其他
        将n设置为-1
    万一
    设置的AppleScript的文本项分隔符:
    MYNAME设置为文本mypath中的项n,
    如果(MYNAME包含。),然后
        设置的AppleScript的文本项分隔符。
        MYNAME设置为文本1直通MYNAME的文本项-2
    万一
    设置的AppleScript的文本项分隔符为
    返回MYNAME
结束getMyName

How do I find the name of an executing AppleScript?

REASON: I want to create a script that changes its behavior based on its filename. Something like:

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

The normal way to get the name is by using "name of me". However applescripts are run by applescript runner so when you use that on a script you get "Applescript Runner" as the name. If you compile your script as an application then "name of me" will work. The only way to get the script name is by getting its path and extracting the name. Something like this would thus work for scripts...

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