What is missing in AppleScript attempt?

Hello, I am an ignoramus.
I would like to open the pages app → save all open documents and replace when asked → close the document
I timidly came up with this script which obviously does not work.

tell application "Pages"
activate
tell application id "com.apple.iWork.Pages" to close every document window saving yes
end tell

thanks in advance for your time and help

I am guessing but maybe this?

tell application "Pages"
    activate
    tell application id "com.apple.iWork.Pages" to close every document saving yes
end tell
1 Like

thank you for your reply. No it does not work. Docs still open in Pages

I am not clear on why the tell within a tell or the need to activate if you are closing documents and saving with no prompt to bring to the fore.

Would just a one liner to close and save all document suffice?

tell application "Pages" to close every document saving yes

This leaves Pages open, so if you wanted to quit the app too, then maybe this.

tell application "Pages"
	close every document saving yes
	quit
end tell

Or if you then wanted to close open documents and create a new one, maybe then the activation would come in?

tell application "Pages"
	close every document saving yes
	make new document
	activate
end tell

Of maybe opening an existing file that you choose?

tell application "Pages"
	close every document saving yes
	activate
	open file (choose file)
end tell

Maybe one of those better meets your needs, or you could clarify what you are trying to do? You noted initially you were opening Pages to then close and save files, but if the application was not open the files would have been explicitly saved or lost and only if it was open and just needed activating is there any chance the files would need saving, which brings us back to why the need to activate?

1 Like

I am very grateful for your detailed reply.
Yes, you are right: I should have explained the context.
I tested all of the scripts and in no case are the files saved as .pages files, so the scripts did not work for a reason that I still have to elucidate.

Here is the context if you have the patience.

I use Bear (Markdown) app a lot, and in the final stages, when I have to distribute the technical notes I write, I export the notes to RTFD format and must as a last step convert those files to Pages.

If I export all the Bear notes to RTFD → select the files → either open with Pages or drag and drop on to the Pages icon in the dock, all of the RTFD notes are converted to Pages including nice format, images and the first page of any inline PDF.

My next step would be to Cmd S each file → click on the save button → repeat for each file.

The impetus for this post was to skip the tedious manual last step, ie saving one file after another manually.

The fact the script does not work probably has something to do with the fact that files were RTFD converts, but I am just guessing.

thank you again very much

I tested the scripts before posting and they did work. However, I was not starting from RTFD files. This is quite a key aspect to to leave out! Also the fact that you are first exporting from somewhere else means that there is potentially more automation that could be done. Do please consider sharing all the details when posting a query. Irrelevant information is easy to discard, but relevant information can’t be made up.

Bear does not have AppleScript support, but it does have Shortcuts support, so you could potentially even start from there in your automation to export to RTFD and pass that on to another stage in your conversion process.

That’s where I’ve picked it up as I could bundle that into something and let you pick it apart and build it out from there (remember that Shortcuts supports running AppleScript and calling scripts at the command line (AppleScript can be run at the command line).

I’m not at all fond of AppleScript, but it does seem to be the best option for doing the conversion with Pages, and unfortunately textutil and pandoc are not helpful in this case as far as I can tell. So I persisted and cobbled together something that seems to work for a test note I’ve exported from Bear as RTFD and then used this AppleScript to convert automatically into a Pages format document. There was some interesting research along the way because Pages has an issue where it throws up permissions errors when trying to export via AppleScript - an issue that’s apparently been around since 2017 at least. Fortunately, someone had come up with a clever workaround.

The resulting script I’ve built into an Automator workflow set as a Quick Action. You can download it here as a ZIP file. Unzip it and copy the workflow file into your Services directory (~/Library/Services/RTFD to Pages.workflow). Then make sure it is enabled in Settings (App) > Extensions > Finder.

In Finder, right click on your RTFD, and select “RTFD to Pages”. With any luck (maybe after a few permissions prompts), you should get a .pages version of your file in the same directory. The original RTFD is left in place.

Here’s how the automation and script look.

Maybe there’s a better way? As I noted, AppleScript isn’t really my favourite as I have more of a traditional developer background so it doesn’t gel with me the way most other types of languages do. There may be issues with it, but honestly it’s the best first stab I can make at something that I would have expected to be a lot simpler than it turned out to be. I’ve also carried out a very limited simple file test (though the file did contain both images and text).

Give it a try, and let us know if it works or if this leads to any other issues.

1 Like

Thank you VERY much for the detailed message and script!

Since I use Keyboard Maestro a lot, I copied the script and pasted into an AppleScript Keyboard Maestro macro called " “ZZ Pages Save All”" (which I which I put in my Keyboard Maestro Finder group) → selected 2 rtfd files in Finder → ran the macro, and got the following error in the Keyboard Maestro engine

2024-04-27 05:15:24 Execute an AppleScript failed with script error: text-script: execution error: {} doesn’t match the parameters {input, parameters} for run. (-1721). Macro “ZZ Pages Save All” cancelled (while executing Execute AppleScript).

thanks again very much

That would make sense, because it sounds like you just copied the AppleScript that was set to take input in Automator and pasted it into a macro in Keyboard Maestro where you presumably didn’t set it up to pass the inputs as expected. We have to assume this as you have not shared what exactly you set up, just a brief text description. But while a lot of scripting is just write once run anywhere, you do need to plumb a lot of them in to use them. Again, if this was something you desperately wanted to run from Keyboard Maestro rather than anywhere else, then that is key information to share from the start.

Some suggestions and considerations for you:

  1. Try the Automator quick action I built and check it works. You could then use that if it does, and if it doesn’t, you know the impact on trying to modify it in Keyboard Maestro, and there is probably little benefit from proceeding at all.
  2. Consider if “using Keyboard Maestro a lot” means everything should be done in Keyboard Maestro.
  3. Modifying the AppleScript to work with Keyboard Maestro might be a good learning opportunity for you - it doesn’t sound like you tried to fix it, or at least you didn’t share with us what you did try if you did attempt to. Start with understanding what the script does, then focus on what it is expecting as a parameter.
  4. If you really do desperately want to use Keyboard Maestro, did you know that quick actions appear in an Application’s Services menu and that Keyboard Maestro can trigger menu items?
  5. If you just want to be able to trigger via a keyboard shortcut, did you know that under the OS keyboard shortcuts settings you can access and assign shortcuts to services?

Hopefully that give you a number of things you can now do independently that will give you the options you want within the requirements you have set out to this point.

1 Like

I will work through your list. You are extremely helpful. You didactic replies are greatly appreciated.

Hi, just to let you know that I have been working with your automator workflow. It is both extremely fast and effective. I could not ask for more. Superb !
I was stubbornly trying to fit it in with Keyboard Maestro which was absurd. It works fine with automator and the services menu.
I would also like to apologize for my confusing post, incomplete information and other issues that must have tested your patience.
Have a nice day!

1 Like