Trying to help an user, I wrote a script in which I borrowed an old Nigel Garvey's handler.
This one was described as "written for Tiger" so I was not surprised to see that it fails under Leopard.
I was surprised to discover that it works correctly under Snow Leopard.
This behaviour was puzzling me.
I wrote a stripped script to test easily.
Here is the code
--{code}
--[SCRIPT]
set theApp to my getProcessName()
tell application (path to frontmost application as string) to display dialog "" & theApp
--=====
on parleAnglais()
local z
try
tell application "Preview" to set z to localized string "Cancel"
on error
set z to "Cancel"
end try
return (z is not "Annuler")
end parleAnglais
--=====
on getProcessName()
(*
This handler by Nigel Garvey behaves well under 10.4 and 10.6.
It returns the script's name under 10.5
*)
if ((path to me) is (path to frontmost application)) then
-- The script's running as an applet, from a script editor window, or from an application's Scripts menu.
-- The user will already been warned not to use the last method.
-- Set the applet/editor's visible to false to stop it being frontmost.
tell application "System Events" to set visible of (first application process whose frontmost is true) to false
-- If the applet was started by double-clicking its icon, the Finder will probably (though not necessarily)
-- now be the frontmost app. Check with the user before setting its frontmost to false.
tell application "Finder"
if (frontmost) then
if my parleAnglais() then
set bouton3 to "Double-clicked"
display dialog "The frontmost application is the Finder. Is that just because you double-clicked this script or are you seriously expecting to print from the Finder?" buttons {"Cancel", "I'm a dip head", bouton3} default button 3 cancel button 1 with title "Preview" with icon caution
else
set bouton3 to "Double-cliqué"
display dialog "Le Finder est au premier plan. Est ce parce que vous avez double-cliqué le script ou tenez-vous vraiment à afficher depuis le Finder?" buttons {"Annuler", "J’y tiens", bouton3} default button 3 cancel button 1 with title "Prévisualiser" with icon caution
end if -- parleAnglais…
--if (button returned of the result is bouton3) then set frontmost to false
if (button returned of the result is bouton3) then tell application "System Events" to tell application process "Finder" to set {visible, visible} to {false, true}
end if -- (frontmost)…
end tell -- Finder
else
-- The script's running from either Script Menu or FastScripts. If FastScripts, the target application's already frontmost.
tell application "System Events" to set ImInScriptMenu to (name of (first application process whose frontmost is true) is "System Events")
if (ImInScriptMenu) then
-- If running from Script Menu, create and open a script applet to nudge System Events from the frontmost slot.
set nudge to ((path to temporary items from user domain as Unicode text) & "Nudge.app")
run script "script o
tell app \"System Events\"
repeat until (last application process is not visible)
delay 0.2
end repeat
end tell
end script
store script o in file \"" & nudge & "\" replacing yes"
tell application "System Events"
set eoap to (count application processes) + 1
-- Open the created applet and wait for it to appear at the end of the application processes.
open file nudge
repeat until (application process eoap exists)
delay 0.2
end repeat
-- Set its visible to false to vacate the 'frontmost' slot in favour of the target app.
set visible of application process eoap to false
-- The applet will finish when it sees it's invisible. Wait for it to quit, then zap its file.
repeat while (application process eoap exists)
delay 0.2
end repeat
delete file nudge
end tell -- System Events
end if -- (ImInScriptMenu)
end if -- ((path to me)…
-- Return the name of the now frontmost target applicaton process.
tell application "System Events" to return name of first process whose frontmost is true
end getProcessName
--=====
--[/SCRIPT]
--{code}
When the script is saved as an Application package, under 10.4.x or 10.6.x, it returns correctly the name of the application which was at front before triggering the script.
Under 10.5.x, it returns the name of the script.
I made a lot of experiments which I will not detail here.
The result is that, to get the wanted behaviour, we must add one instruction at the very beginning of the handler.
This instruction is :
tell application "System Events" to (get path to frontmost application) (* WRONG ONE * )
With it, the original first instruction behaves as wanted.
My guess is that, when entering the handler, a structure is undefined or wrongly defined and that this oddity is cured by the added instruction.
Did you already encountered this kind of behaviour which puzzled me during some days?
Yvan KOENIG (VALLAURIS, France) dimanche 16 mai 2010 17:46:18