Why doesn't this applescript seem to work as expected?
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 AppleSho开发者_StackOverflow社区wAllFiles") 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.
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...
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
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.
-- 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
EDIT: It seems your step #2 is incorrect. You need to use "ON" or "OFF" instead of 0 or 1. Try this...
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
A slight modification of @regulus6633’s script works on my machine:
-- 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
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…
精彩评论