Get front app from OS X script menu
Get front app from OS X script menu
- Subject: Get front app from OS X script menu
- From: Paul Berkowitz <email@hidden>
- Date: Wed, 09 Jan 2002 02:18:51 -0800
A few people have mentioned how it is impossible to get what should be the
front application from a script running in OS X's ScriptMenu, since the act
of running the script puts "SystemUIServer" in front every time, and
AppleScript does not have a method of getting the next layer down. I imagine
that there must be a nifty way to do this scripting Terminal, but I don't
know any Unix. Here's a way to do it:
Have a stay-open applet to return the front application's path or name to
any script run for ScriptMenu, atfer it tells "SystemUIServer" to quit, then
relaunches it at the end. The script in ScriptMenu calls a handler in the
applet 'ignoring application responses'. Here is a demonstration template
for a compiled ScriptMenu script called "Front App.scpt" - insert your own
script at the appropriate point near the end. This is followed by the script
of the stay-open applet , called here "Front App Server.app".
---------------Compiled Script "Front App.scpt" in ScriptMenu--------------
property myName : "Front App.scpt" --MUST put the name of the script here
property frontAppApp : ""
property currentAppPath : ""
on run
if frontAppApp = "" then
set frontAppApp to choose file of type {"APPL"} with prompt "Where
is the Front App Server applet?"
display dialog "The script is now set up." & return & return & "From
now on, the script will run normally, accessing the front application via
the applet." & return & return & "You can move the applet wherever you
want." buttons {"OK"} default button 1 with icon 1
return
end if
if currentAppPath = "" then -- just called
set appletPath to frontAppApp as string
ignoring application responses
tell application appletPath
launch
PassFrontApp(myName)
end tell
end ignoring
else -- frontAppApp has loaded and stored the path to the front
application in currentAppPath
set {oldDelims, AppleScript's text item delimiters} to
{AppleScript's text item delimiters, {":"}}
if currentAppPath ends with ":" then -- a Cocoa "package"
set currentAppName to text item -2 of currentAppPath
else -- Carbon or Classic apps
set currentAppName to text item -1 of currentAppPath
end if
if currentAppName ends with ".app" then set currentAppName to text
1 thru -5 of currentAppName -- optional
set AppleScript's text item delimiters to oldDelims
-- insert your own script here, and omit what follows until after
'end tell'
-- for demonstration
set r to "The front application is currently " & currentAppName &
"." & return & return & "The path to its application file is " &
currentAppPath & "."
tell application currentAppName
display dialog r with icon 1
end tell
-- end of demonstration
set currentAppPath to "" -- reset for next time
end if
end run
---------------------------------end "Front App.scpt"--------------------
-------------------"Front App Server.app" stay-open application----------
on PassFrontApp(scriptName)
tell application "SystemUIServer" to quit
set done to false
tell application "System Events"
repeat until done
set procs to name of every process
if procs does not contain {"SystemUIServer"} then
set done to true
end if
end repeat
--set frontAppName to (name of first process whose frontmost is
true) as string -- and visible is true [options]
end tell
set frontAppPath to path to frontmost application as string
set scriptPath to (path to scripts folder from user domain as string) &
scriptName
set f to load script file scriptPath
set f's currentAppPath to frontAppPath
tell f to run
--don't store
tell application "SystemUIServer" to launch
end PassFrontApp
-----------------------end "Front App Server.app"-------------------
--
Paul Berkowitz