Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How find the file name of an executing AppleScript

Tags:

applescript

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
like image 298
dcblack Avatar asked Jan 20 '23 22:01

dcblack


2 Answers

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
like image 185
regulus6633 Avatar answered Jan 22 '23 11:01

regulus6633


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
like image 39
mklement0 Avatar answered Jan 22 '23 12:01

mklement0