How find the file name of an executing AppleScript

烈酒焚心 提交于 2021-02-08 20:55:21

问题


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

回答1:


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



回答2:


Here's a method that works for all of the following:

  • *.scpt files (compiled AppleScript files; run in AppleScript Editor or with osascript)
  • *.applescript files (uncompiled AppleScript files; run in AppleScript Editor or with osascript)
  • command-line scripts that directly contain AppleScript (marked as executable and starting with #!/usr/bin/env osascript):
  • *.app files created with AppleScript Editor
  • *.app files created with Automator that contain AppleScript actions

Note: By contrast, it does not work for the following:

  • OS X services created with Automator that contain AppleScript actions (special *.workflow files) - always reports 'WorkflowServiceRunner[.xpc]'
  • general-purpose *.workflow files created with Automator that contain ApplesScript actions and that are run with automator - always reports 'Automator Runner[.app]'

Get the name of the running script, including filename extension (.scpt, .app, or .applescript, as the case may be):

tell application "System Events" to set myname to get name of (path to me)

If you want to remove the filename extension with a single command, use the following, do shell script-based approach:

tell application "System Events" to set myname to do shell script "rawName=" & quoted form of (get name of (path to me)) & "; printf '%s' \"${rawName%.*}\""

Here's an all-AppleScript alternative that is more verbose (yet concise by AppleScript standards):

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

Finally, here's a variation: a subroutine that you can call with set myname to getMyName():

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



回答3:


An easier way to find out the base part of the path is using name of:

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



回答4:


Maybe this: set appname to name of current application



来源:https://stackoverflow.com/questions/5770384/how-find-the-file-name-of-an-executing-applescript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!