App open when Pages i open

I have created a script that helps with Apple Pages. Is there a way I can make my app open and active when Pages is open?

The fast and ideal situation would be to call it with a keyboard combination when I am in Pages.

If you’re calling it from a keyboard combination, I don’t understand why you’d need it to launch with Pages, is seems like it could just run from the keyboard combo. What does it do? Is it a stay-open app?

I’d recommend FastScripts to invoke the script from a keyboard combo:

https://red-sweater.com/fastscripts/

If there is some reason it needs to stay open - like it’s a stay-open app that gathers and assembles info from different parts of your document over time or something - then I’d think you could just make it a stay-open app and keep it open all the time, or else let it be called and opened by Fastscripts the first time you invoke the key combo that makes it do whatever it does.

If for some reason you really need it to open simultaneously with Pages launching, you could make a little Applescript app called “Open Pages” or whatever, have it just launch both pages and your app, and use that to open them.

@t.spoon Thank you very much for your answer. I am new and it is very difficult to find information about how all this works.

ALWAYS OPEN

I didn’t know that the app could be always open. To do that I should make export > File Format Application and select Stay open after run handler. Is that the right way?

I want to make the app for other people. So I suppose that I have to use Xcode to make the basic preferences window, buttons… In that case, is there a way to make the app always opened?

KEYBOARD

I want to distribute my app so I cannot use Fastscripts, I suppose. In Xcode I can make a keyboard shortcut, but it only works inside the app, not when the user does not have the app selected. I suppose?

As you can see, I am very lost. I cannot find all those basic information anywhere. I would appreciate very much any orientation

I should have been more accurate in my terminology use, it’s a “Stay Open” app, not an “Always Open App.”

Here are some links with the basics of how those work:
https://macscripter.net/viewtopic.php?id=37846
https://scriptingosx.com/tag/stay-open/

Distributing an Applescript app that has to run on a keyboard command on local systems is a problem that, as far as I know, doesn’t have a simple, straight-forward solution. I actually do that constantly, but it’s in a controlled environment where we installed FastScripts on all of the machines, and the FastScripts copies pull their key bindings from a shared plist file that’s syncronized via Dropbox onto all of the machines. So I can put any script on any key for everybody instantly.

Assuming you can’t install software other than your script on all the user’s machines…

You can use Automator to make your script into a system-wide service:
https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/MakeaSystem-WideService.html

Then in System Preferences → Keyboard → Shortcuts → Services
You can put it on a keyboard shortcut.
You can write an installer script that does this on the user’s system using UI scripting, but that script will have to request permissions to do UI scripting.

https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AutomatetheUserInterface.html

Also, the keyboard shortcut you use will then be global - it could conflict with shortcuts in other apps, and will run when invoked even if the user isn’t in Pages when they hit it. It can be constrained to only run from Pages if triggered from Fastscripts or Quicksilver, but I don’t know of a way to run a script constrained by application from System Preferences.

Good luck, let us know if you have more questions.

Oh, I forgot to mention. If all you need in the way of GUI is one preferences window, then I’d still skip Xcode and just use something like Shanes Stanley’s Dialog Tookit:

https://macosxautomation.com/applescript/apps/Script_Libs.html

That’s what we use for scripts that need preferences.

The way I do it:
If the script itself presents any UI, then give it a “preferences” button that launches another script that provides the preferences UI. If there is no UI for the main script, then just distribute it with a separate “Edit Script Preferences” app that users can just double-click to run.

The preferences app presents whatever UI widgets to do the settings using Dialog Toolkit. When it returns the data, I just write that to a text file (JSON format in my case) inside the script bundle of the script that needs the preferences. That script, in turn, is written to read and use the preferences from that file.

So Xcode isn’t required to use preferences. On the other hand, depending on what you’re doing, it may be the best or easiest way to go, or be required for other features you’re doing.

If you get into Xcode, that’s beyond me, but there are other users here who use it.

@t.spoon: thank you. Your information is very useful. Learning AppleScript I became too excited. With all those limitations with permissions, it seems that AppleScript is more for personal use or controlled small environments and not to make apps for other people.

I’m pretty sure that if you were using Objective C or Swift, you’d be up against the same permissions constraints. All apps need to be added to “Accessibility” in the security and privacy pane in order to control the UI, it’s not just an Applescript thing.

Applescript certainly isn’t intended for writing major applications. That said, the “limitations with permissions” you’re running into is because you’re trying to modify the behavior of other applications (Pages) or the operating system (with a global key command), which is rightfully locked down, or it would be a malware nightmare. It’s not an Applescript thing, it’s a MacOS thing.

Even when this time it is against my idea, I understand, even appreciate the limitations Apple imposes.