For Pages and Numbers at the same time

I have created a script/app for Apple Pages. It works well.

I have duplicated it and change the name Pages for Numbers and it works well for Numbers too. (it is related to a common thing in the inspector.)

Could I use only one script/app to do both? How could I say something like:

tell application “Numbers” or "Pages to activate whatever is in the front…

Perhaps I should check which app is in front first? if so, how?

I use often this simple checking:


-- 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.

Or use bundle identifiers instead of names:

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.

Shane,

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

Thank you all for the answers