为什么这个苹果脚本似乎没有按预期工作?
我正在使用自动化程序编写服务。它在任何应用程序
中都不接收输入
。
它所做的只是运行这个简单的脚本:
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 不会再次激活。
有人可能会说第四个块中的代码一定有问题,但我已经做了一些实验来证明一切都按预期工作:
当我用 not equalequal
时code> 并从非 Finder 的任何应用程序运行脚本,Finder 确实会按应有的方式激活。
所以看来只有当 Finder 位于前面时触发脚本时才会出现问题。
(这就是服务应该做的:在任何应用程序中,切换 Finder 中隐藏文件的可见性。当 Finder 位于前面时,执行脚本后它应该位于前面,当另一个应用程序位于前面时,此应用程序应该位于前面应该仍然在前面。)
我在 Lion 上。
I'm writing a service using automator. It receives no input
in any application
.
All it does is run this simple script:
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
I'll walk you trough it step by step:
first block: get the name of the current frontmost app and store it in a variable app_name
.
second block: toggle the hidden files variable on or of, depending on its value.
third block: run killall Finder
to relaunch Finder, taking the toggle from the second block into effect. Pause 0.5
sec, somehow this is necessary (don't know why, but without this the next instruction will be ignored).
fourth block: Check what the variable app_name
was. If it equals Finder
this means finder was active when the script was initiated, thus activate Finder once more (killall Finder
leaves it in the background).
Problem: Everything works as expected but for one thing: when using this service in the Finder, Finder doesn't get activated again.
One might argue that there must be something wrong with the code in the fourth block, yet I've experimented a bit to show everything works as expected:
When I replace equal
by not equal
and run the script from any app that is not the Finder, Finder DOES get activated as should be.
So it seems that there only is a problem when the script is fired when Finder is in front.
(This is what the service should do: from within any app, toggle the visibility of the hidden files in Finder. When Finder was in front, it should be in front after execution of the cript, when another app was in front, this app should still be in front.)
I'm on Lion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以前也遇到过这个。基本上,您是说,当 Finder 位于最前面时运行此命令时,Finder 并不是真正位于最前面。这是真的,因为你说这是一项自动化服务。我相信自动化程序是由名为“Automator Runner”的应用程序运行的。因此,实际上一旦服务运行,Automator Runner 就会成为最前面的。请注意,它是一个不露面的应用程序,因此您看不到它是最前面的,但它确实是最前面的。因此,当您检查 Finder 是否位于最前面时,它从来都不是。这有道理吗?我在运行 applescript 时看到同样的情况,因为它们是使用 Applescript Runner 运行的。
那么如何解决这个问题呢?这是一个想法。将此作为您的第一个块,看看它是否有帮助...
注意:我不确定 Automator Runner 将是最前面的进程。它可能是其他名称,例如自动操作的名称。但是您可以确定其他东西是最前面的,因为自动操作正在运行......所以如果我的代码不起作用,那么您只需找出自动操作运行时正在运行的进程的名称并将其放入进入代码。您始终可以在代码中放置一个“显示对话框”来显示最前面进程的名称。
另一个提示。一般来说,如果可以使用 QUIT 命令代替,我不喜欢使用 KILLALL 命令。 Quit 专为 Mac 设计,可确保正常停止操作。幸运的是,Finder 有一个退出命令。在你的第三个和第四个块上尝试这个。您会看到,如果 Finder 位于最前面,那么我们会激活它,这会使其再次位于最前面,但如果不是,那么我们会启动它,这样它至少会再次运行,但不会出现在最前面。
编辑:看来您的第 2 步不正确。您需要使用“ON”或“OFF”而不是0或1。试试这个...
I have come across this before too. Basically you're saying that when this gets run when the Finder is frontmost that the Finder is not really frontmost. And that is true because you said this is an automator service. I believe automator stuff is run by an application named "Automator Runner". So actually as soon as the service is run Automator Runner becomes frontmost. Note that it is a faceless application so you can't see that it's frontmost but it is. So when you check if the Finder is frontmost it never is. Does that make sense? I see the same thing when running applescripts because they're run using Applescript Runner.
So how do you fix this? Here's a thought. Make this your FIRST BLOCK and see if it helps...
NOTE: I'm not certain that Automator Runner will be the frontmost process. It may be something else like the name of your automator action. But you can be certain something else is frontmost because of the automator action being run... so if my code doesn't work then you just have to figure out the name of the process that's running when your automator action is running and put that into the code. You can always put a "display dialog" in your code to show you the name of the frontmost process.
One other tip. In general I do not like to use the KILLALL command if I can use a QUIT command instead. Quit is designed for the Mac and makes sure things are stopped gracefully. As luck would have it the Finder has a quit command. Try this for your 3rd and 4th blocks. You'll see that if the Finder was frontmost then we activate it which makes it frontmost again but if it wasn't then we launch it so it is at least running again but doesn't come to the front.
EDIT: It seems your step #2 is incorrect. You need to use "ON" or "OFF" instead of 0 or 1. Try this...
对 @regulus6633 的脚本进行轻微修改可以在我的机器上运行:
我必须承认我并不完全确定原因和方式 - 启动和activate 命令在 Finder 的情况下似乎不够......
A slight modification of @regulus6633’s script works on my machine:
I must admit I am not entirely sure of the why and how – the AppleScript documentation for the launch and activate commands does not seem to be adequate in Finder’s case…