Tuesday, April 13, 2021

#1 2021-04-06 04:19:33 am

NMG
Member
Registered: 2019-09-29
Posts: 53

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?

Last edited by NMG (2021-04-06 04:33:33 am)

Offline

 

#2 2021-04-06 05:36:03 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1744

Re: For Pages and Numbers at the same time

NMG wrote:

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


I use often this simple checking:

Applescript:


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

Last edited by KniazidisR (2021-04-06 06:06:15 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2021-04-06 07:17:15 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6613

Re: For Pages and Numbers at the same time

KniazidisR wrote:

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:


Applescript:

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 Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2021-04-06 08:00:09 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1744

Re: For Pages and Numbers at the same time

Shane,

I will improve your script, which will be better than mine if it detects the identifiers of the installed script editors automatically:

Applescript:


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

Last edited by KniazidisR (2021-04-06 08:09:27 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2021-04-07 05:41:38 am

NMG
Member
Registered: 2019-09-29
Posts: 53

Re: For Pages and Numbers at the same time

Thank you all for the answers

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)