-- check: get name of frontmost application
tell application "System Events"
set myFrontMost to name of the first process whose frontmost is true
if myFrontMost is in {"Script Editor", "Script Debugger"} then
set visible of process myFrontMost to false
-- Get name of app, which was frontmost before the script editor
set myFrontMost to name of the first process whose frontmost is true
end if
end tell
-- then, you continue with frontmost application
if myFrontMost is in {"Pages", "Numbers"} then
using terms from application "Numbers"
tell application myFrontMost
save front document as PDF
end tell
end using terms from
end if
NOTE: This method targets applications that have the same name as the process (like this case, with Numbers.app and Pages.app). For other applications you need additional step - associate process names with application names yourself.
tell application "System Events"
set myFrontMost to bundle identifier of the first process whose frontmost is true
if myFrontMost is in {"com.latenightsw.ScriptDebugger8", "com.apple.ScriptEditor2"} then
set visible of process 1 whose bundle identifier is myFrontMost to false
-- Get name of app, which was frontmost before the script editor
set myFrontMost to bundle identifier of the first process whose frontmost is true
end if
end tell
-- then, you continue with frontmost application
if myFrontMost is in {"com.apple.iWork.Pages", "com.apple.iWork.Numbers"} then
using terms from application "Numbers"
tell application id myFrontMost
save front document as PDF
end tell
end using terms from
end if
Note that using terms compiles to by-name even if you use id there, because it needs to be resolved at compile time rather than run time.
I will improve your script, which will be better than mine if it detects the identifiers of the installed script editors automatically:
-- determine correct bundle IDs of installed script editors
tell application "System Events" to set ScriptEditorBundleID to id of application "Script Editor"
set excludedBundleIDsList to ScriptEditorBundleID as list
try
tell application "System Events" to set ScriptDebuggerBundleID to id of application "Script Debugger"
set end of excludedBundleIDsList to ScriptDebuggerBundleID
end try
-- check: get bundle ID of frontmost application
tell application "System Events"
set myFrontMost to bundle identifier of the first process whose frontmost is true
if myFrontMost is in excludedBundleIDsList then
set visible of process 1 whose bundle identifier is myFrontMost to false
-- Get bundle id of app, which was frontmost before the script editor
set myFrontMost to bundle identifier of the first process whose frontmost is true
end if
end tell
-- then, you continue with frontmost application
if myFrontMost is in {"com.apple.iWork.Pages", "com.apple.iWork.Numbers"} then
using terms from application "Numbers"
tell application id myFrontMost
save front document as PDF
end tell
end using terms from
end if