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

发布于 2024-11-03 03:06:14 字数 182 浏览 1 评论 0原文

如何找到正在执行的 AppleScript 的名称?

原因:我想创建一个根据文件名更改其行为的脚本。像这样的东西:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若沐 2024-11-10 03:06:14

获取名字的正常方法是使用“我的名字”。然而,applescripts 是由 applescript runner 运行的,因此当您在脚本上使用它时,您会得到“Applescript Runner”作为名称。如果您将脚本编译为应用程序,那么“我的名字”将起作用。获取脚本名称的唯一方法是获取其路径并提取名称。因此,这样的东西适用于脚本......

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

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
や莫失莫忘 2024-11-10 03:06:14

以下是适用于以下所有内容的方法:

  • *.scpt 文件(已编译的 AppleScript 文件;在 AppleScript 编辑器中运行或使用 osascript 运行)
  • < code>*.applescript 文件(未编译的 AppleScript 文件;在 AppleScript 编辑器中运行或使用 osascript 运行)
  • 直接包含 AppleScript 的命令行脚本(标记为可执行文件并以#!/usr/bin/env osascript):
  • 使用 AppleScript 编辑器创建的 *.app 文件
  • *.app 使用 Automator 创建的包含 AppleScript 的文件注意

:相比之下,它不适用于以下情况:

  • 使用 Automator 创建的包含 AppleScript 操作(特殊的 *.workflow 文件)的 macOS 服务) - 他们总是报告 使用 Automator 创建的WorkflowServiceRunner[.xpc]
  • 通用 *.workflow 文件,其中包含 ApplesScript 操作并使用 automator 运行 - 它们始终报告Automator Runner[.app]

获取正在运行的脚本的名称,包括文件扩展名(.scpt.app. applescript,视情况而定可能是):

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

如果您想使用单个命令删除文件扩展名,请使用以下基于 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 更简洁)标准):

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

最后,这是一个变体:您可以通过 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

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:

  • macOS services created with Automator that contain AppleScript actions (special *.workflow files) - they always report WorkflowServiceRunner[.xpc]
  • general-purpose *.workflow files created with Automator that contain ApplesScript actions and that are run with automator - they 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
心奴独伤 2024-11-10 03:06:14

查找路径基础部分的更简单方法是使用 name of

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

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
谁许谁一生繁华 2024-11-10 03:06:14

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

Maybe this:
set appname to name of current application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文