2

我正在使用自动机编写服务。它接收no input.any application

它所做的只是运行这个简单的脚本:

on run {input, parameters}
--FIRST BLOCK
    tell application "System Events"
        set app_name to name of the first process whose frontmost is true
    end tell
--SECOND BLOCK
    if (do shell script "defaults read com.apple.finder AppleShowAllFiles") is equal to "0" then
        do shell script "defaults write com.apple.finder AppleShowAllFiles 1"
    else
        do shell script "defaults write com.apple.finder AppleShowAllFiles 0"
    end if
--THIRD BLOCK
    do shell script "killall Finder"
    delay 0.5
--FOURTH BLOCK
    if (app_name is equal to "Finder") then
        tell application "Finder"
            activate
        end tell
    end if
end run

我会一步一步地引导你:

第一个块:获取当前最前面的应用程序的名称并将其存储在一个变量app_name中。

第二块:根据其值打开或关闭隐藏文件变量。

第三块:运行killall Finder以重新启动 Finder,使第二块的切换生效。暂停0.5秒,不知何故这是必要的(不知道为什么,但如果没有这个,下一条指令将被忽略)。

第四块:检查变量app_name是什么。如果它等于Finder这意味着 finder 在脚本启动时处于活动状态,因此再次激活 Finder(killall Finder将其留在后台)。

问题:一切都按预期工作,但有一件事:在 Finder 中使用此服务时,Finder 不会再次被激活。

有人可能会争辩说第四块中的代码一定有问题,但我已经做了一些实验来证明一切都按预期工作:

equal当我从任何不是 Finder 的应用程序中替换not equal并运行脚本时,Finder 会按应有的方式被激活。

所以似乎只有当Finder在前面时触发脚本时才会出现问题。

(这是服务应该做的:从任何应用程序中,切换 Finder 中隐藏文件的可见性。当 Finder 在前面时,它应该在执行脚本后在前面,当另一个应用程序在前面时,这个应用程序应该还在前面。)

我在狮子。

4

2 回答 2

1

我以前也遇到过这种情况。基本上,您是说当 Finder 位于最前面时运行它时,Finder 并不是真正位于最前面。这是真的,因为你说这是一项自动化服务。我相信 automator 的东西是由一个名为“Automator Runner”的应用程序运行的。所以实际上只要服务运行 Automator Runner 就成为最前面的。请注意,它是一个不露面的应用程序,因此您看不到它在最前面,但它确实是。因此,当您检查 Finder 是否位于最前面时,它永远不会。那有意义吗?我在运行 applescripts 时看到了同样的情况,因为它们是使用 Applescript Runner 运行的。

那么你如何解决这个问题?这是一个想法。把它作为你的第一个块,看看它是否有帮助......

tell application "System Events"
    set app_name to name of the first process whose frontmost is true
    if app_name is "Automator Runner" then
        set visible of process "Automator Runner" to false
        set app_name to name of first process whose frontmost is true
    end if
end tell

注意:我不确定 Automator Runner 是否会是最前面的进程。它可能是其他东西,例如您的自动化操作的名称。但是你可以确定其他的东西是最前面的,因为正在运行的自动化操作......所以如果我的代码不起作用,那么你只需要弄清楚当你的自动化操作运行时正在运行的进程的名称并把它进入代码。您始终可以在代码中放置一个“显示对话框”来显示最前面的进程的名称。

另一个提示。一般来说,如果我可以使用 QUIT 命令,我不喜欢使用 KILLALL 命令。Quit 专为 Mac 设计,可确保优雅地停止操作。幸运的是,Finder 有一个退出命令。试试这个为你的第 3 和第 4 块。您会看到,如果 Finder 位于最前面,那么我们将其激活,使其再次位于最前面,但如果不是,则我们启动它,因此它至少会再次运行但不会出现在最前面。

-- quit the Finder
tell application "Finder" to quit

-- delay until the Finder quits
repeat
    try
        tell application "System Events" to get first process whose name is "Finder"
        delay 0.1
    on error
        exit repeat
    end try
end repeat

-- restart the Finder
if (app_name is equal to "Finder") then
    tell application "Finder" to activate
else
    tell application "Finder" to launch
end if

编辑:看来你的第 2 步是不正确的。您需要使用“ON”或“OFF”而不是 0 或 1。试试这个...

if (do shell script "defaults read com.apple.finder AppleShowAllFiles") is equal to "ON" then
    do shell script "defaults write com.apple.finder AppleShowAllFiles OFF"
else
    do shell script "defaults write com.apple.finder AppleShowAllFiles ON"
end if
于 2011-09-19T21:31:33.900 回答
0

对@regulus6633 的脚本稍作修改可以在我的机器上运行:

-- restart the Finder
tell application "Finder" to launch      # always launch
if (app_name is equal to "Finder") then
  tell application "Finder" to activate  # activate separately
end if

我必须承认我并不完全确定为什么以及如何 - 用于启动激活命令的 AppleScript 文档在 Finder 的情况下似乎不够......</p>

于 2011-10-20T22:49:51.727 回答