Automation & Scripting

RSS for tag

Learn about scripting languages and automation frameworks available on the platform to automate repetitive tasks.

Automation & Scripting Documentation

Post

Replies

Boosts

Views

Activity

How to access comments and their associated text in iWork documents via AppleScript/ScriptingBridge?
Hi all, I’m developing a Mac OS application with XCode that interacts with iWork documents (Pages, Numbers, Keynote) using ScriptingBridge (and maybe AppleScript). Right now I started with Pages, assuming if it works for Pages, it will likely be similar for Numbers and Keynote. While I can successfully access and modify the main body text (e.g. the “body text” property in a Pages document), I’m having major difficulties accessing the comments (or annotations) within these documents. There are many aspects, but right now what I’m trying to achieve: For a Pages document, I need to scan the document and extract, for each comment: The content of the comment (i.e. the comment’s text). The text that is being commented on. For Numbers, similarly, I need to retrieve the commented cell’s content and the associated comment. For Keynote, the same as Pages, except I manage to get the Presenter Notes. Once done, I could replace the content accordingly. What I’ve tried: Using AppleScript commands such as: every comment of document "Test" Accessing properties like content or range of a comment. Attempting various syntaxes (including using class specifiers) to force AppleScript to recognize comments. Using ScriptingBridge in my Swift code, but I couldn’t find any mapping for a “comment” object in the Pages dictionary. However, all these attempts result in errors such as “cannot convert …” or “this class is not key value coding-compliant for the key …” which leads me to believe that the iWork scripting dictionaries may not expose comments (or annotations) in a scriptable way. Questions: Is there a supported way to access the comments (and the associated commented text) in an iWork document via AppleScript or ScriptingBridge? If so, what is the proper syntax or property name to use? (For example, should I be looking for a class named “comment”, “annotation”, or perhaps something else?) If direct access via AppleScript/ScriptingBridge is not possible, what alternative approaches would you recommend for programmatically extracting comment data from iWork documents? I apologize if my post isn't clear, it is translated from French. Any insights or examples would be greatly appreciated. Thank you!
0
0
82
1d
HomeKit Location issues when the First Person comes home
I want to create automations when the first person comes Home. When Setting up the Automation on the owner device everything seems to be correct. Yet the Automation doesn‘t Wort properly. my girlfriend is listed as admin. when she has a Look at the Automation, the Location of my phone is unknown. the issue is that this leads To the following behaviour. When i come House the Automation Checks if my girlfriend is Home. All Good. When my Grilfriend comes Home the Automation doesn‘t Check where i am But directly execut the Automation.
1
0
131
2d
struggling with app automation
hi so im trying to create a simple app that has two pressable buttons that turn a spa mode on and off wishing another app so far I have the UI figured out the app works the only issue is right now when I press spa on I have setup so it opens Siri Shortcuts and enables the shortcut is there a way to make the app revert back to mine after the action is done? or is there another way to open the other app and navigate to the button within that app and enable it behind my app
0
0
126
4d
Add the ability to remove/delete chats in the ChatGPT app using shortcuts
So Recently I’ve been making shortcuts and I noticed that I can prompt chatgpt with shortcuts to add extra functionality, I like this idea but there is 1 flaw with it and that is there is no ability to remove chats automatically, I suppose this would be fine except every time the shortcut is run it creates a new chat, usually I would have made it to remove the chat after the shortcut is finished, but since I can’t the chats would pile up as there is no way of getting rid of them automatically and it would be tedious to manually delete all the chats. also sorry if I chose the wrong topic and subtopic, I couldn’t find a topic about shortcuts
1
0
185
6d
Automator error when running Watch Me Do-created script
Hi everyone, I'm trying to use Automator to batch process PDF files. I have hundreds of academic journal article PDFs whose page sizes vary from 5x7 inches to A4 format (8.27 x 11.69 inches). I want to scale all the PDFs to US Letter size (8.5 x 11.0 inches) such that smaller originals remain 100% scale but are centered on the larger page and larger originals are scaled down to fit the page. Manually opening a PDF in Preview.app, scaling it to US Letter paper, and using the Save as PDF option is producing the desired output for me. I captured my workflow using the Watch Me Do action in Automator, then adjusted it into the following AppleScript. -- a Get Specified Finder Items action to specify the input PDFs precedes this script on run {input, parameters} repeat with filePath in input -- Open the file in Preview tell application "Preview" open filePath activate end tell -- Give Preview some time to open the file delay 2.0 -- Press ⌘P set timeoutSeconds to 0.25 set uiScript to "keystroke \"p\" using command down" my doWithTimeout(uiScript, timeoutSeconds) -- Click the “Scale to Fit:” radio button. delay 2.0 set timeoutSeconds to 2.0 set uiScript to "click radio button \"Scale to Fit:\" of radio group 1 of group 1 of group 2 of scroll area 2 of splitter group 1 of sheet 1 of window 1 of application process \"Preview\"" my doWithTimeout(uiScript, timeoutSeconds) -- Click the “<fill in title>” menu button. delay 4.0 set timeoutSeconds to 2.000000 set uiScript to "click menu button 1 of group 2 of splitter group 1 of sheet 1 of window 1 of application process \"Preview\"" my doWithTimeout( uiScript, timeoutSeconds ) -- Save as PDF… delay 2.0 set timeoutSeconds to 2.0 set uiScript to "click menu item 2 of menu 1 of splitter group 1 of sheet 1 of window 1 of application process \"Preview\"" my doWithTimeout(uiScript, timeoutSeconds) -- Click the “Save” button. delay 8.0 set timeoutSeconds to 2.0 set uiScript to "click UI Element \"Save\" of sheet 1 of sheet 1 of window 1 of application process \"Preview\"" my doWithTimeout(uiScript, timeoutSeconds) -- Press ⌘W to close the file delay 0.25 set timeoutSeconds to 2.0 set uiScript to "keystroke \"w\" using command down" my doWithTimeout(uiScript, timeoutSeconds) end repeat return input end run on doWithTimeout(uiScript, timeoutSeconds) set endDate to (current date) + timeoutSeconds repeat try run script "tell application \"System Events\" " & uiScript & " end tell" exit repeat on error errorMessage if ((current date) > endDate) then error "Can not " & uiScript end if end try end repeat end doWithTimeout My problem arises at the Save as PDF step. When this action runs, I see the PDF menu pop open: but the Save as PDF... menu item doesn't get clicked. Instead, I get an error: Can anyone advise on how to overcome this error?
1
0
159
1w
"Not authorized to send Apple events to Terminal
We are trying to open an application "xyz.app" It worked fine until 15.1.1 versions. But facing issues with 15.2 and 15.3 The application is working fine when we navigate to xyz.app/Contents/MacOS/ and run applet in this directory. But the error "Not authorized to send Apple events to Terminal" occurs when we are trying to open the app directly. We have tried with all the available solutions like giving full disk access to terminal and application, adding my application to automation in privacy and security tabs in settings. Any help would be appreciated. Thanks!
1
0
151
1w
Check to make sure iTunes is playing - AppleEvent timed out. (-1712)
Hi folks, I've got some music that I want playing on iTunes all the time on an older system, but it'll sometimes stop. I tried making a Applescript to check and play the music/playlist again if it stops, but I keep getting a timeout error. This is the AppleScript: repeat tell application "iTunes" if player state is paused then tell application "iTunes" to play end if delay 30 end tell end repeat I get this error: AppleEvent timed out. iTunes got an error: AppleEvent timed out. (-1712) I can't figure out why I'm getting a timeout error... anyone have any ideas?
1
0
177
2w
AppleScript help
Hello everyone, I would like to use AppleScript to transform a .csv file. To make things easier to understand, I'm attaching two files: 1- The original file in csv format 2- The file as I'd like it to look after I've run it through the script. Here are the steps involved 1-Open the file in numbers (Note: the file is located in the download folder). 2-Delete the first 6 lines 3-Delete all font styles and cell colors 4-Combine all cells in the nature of operation column of the same operation belonging to the same date in the first cell of the operation, deleting all spaces in the text is not necessary for each operation. 5- Delete all empty lines. I hope I've made my request clear. If any of you have the knowledge to do this, if it can be done at all, I'd be very grateful for their help in writing the script. Thank you in advance. 1.csv 2.csv
0
0
191
2w
Changing pad colour in Image Events
I'm trying to use Image Events instead of Photoshop to manipulate a bunch of images. I need to extend the canvas and have the padding be white. I've tried pad theImage to dimensions {545, 545} with pad color {65535, 65535, 65535} But that does nothing. If I remove the 'with pad colour...' part, it works but the pad defaults to black. I've looked everywhere, but there doesn't seem to be a solution. Is there one?
0
0
173
3w
AppleScript nd adobe indesign
I have a bunch of images sized 540px on the long side (some portrait, some landscape). The other side could be any size, but will be less than 540. There are no square images. I want to be able to choose a folder containing the images, and want the script to do the following for each image: Place it at the center of the current page of the active indesign document. Scrape the filename from the image (without the extension), apply the pre-existing Paragraph Style 'Caption' to the text, and center the text 36 points (not pixels) below the image. Group the image and the text. Then create a frame with white fill, 540x600 pixels in size, send it to back, center it with the group created above, and group them. I've been pulling my hair out. Just can't get it to work.
0
0
209
Jan ’25
Help with app automation permissions
Hi, I am trying to make an app that uses Spotify's web API to play songs. For the web API to work, Spotify needs to be running, and my Mac has to be recognized as an active device. For my Mac to be recognized as an active device, I have to play a song for a very short amount of time (under a second). I want to make my app automatically do that on launch. I already wrote the AppleScript in Automator, and it worked. It successfully launched Spotify, played a song for 0.5 seconds, then hid itself. After writing the code, I tried to implement it into my app to run on startup, but I ran into a problem. The app only started the Spotify app on my mac, and gave me an error that told me it wasn't running. What do I do? Is this an issue with the permissions of the app, or something else? I have given the app the "Apple Events" entitlement. This is the error I am getting. Note that the app opens Spotify, after which it gives me this. Error: { NSAppleScriptErrorAppName = Spotify; NSAppleScriptErrorBriefMessage = "Application isn\U2019t running."; NSAppleScriptErrorMessage = "Spotify got an error: Application isn\U2019t running."; NSAppleScriptErrorNumber = "-600"; NSAppleScriptErrorRange = "NSRange: {31, 8}"; } This is the function I am trying to use to do the actions with Spotify: func runAppleScript() { let appleScript = """ tell application "Spotify" activate if player state is not playing then play track "spotify:track:5XSKC4d0y0DfcGbvDOiL93" delay 1 pause end if end tell tell application "System Events" tell process "Spotify" set frontmost to true delay 1 keystroke "h" using {command down} end tell end tell """ var error: NSDictionary? if let scriptObject = NSAppleScript(source: appleScript) { scriptObject.executeAndReturnError(&error) } if let error = error { print("Error: \(error)") } } Any help is appreciated. Thank you in advance.
1
0
287
Jan ’25
Apple Mail and Drag&Drop
I have been trying to accept drops from Apple Mail which do supply data with type identifiers : com.apple.mail.email The problem is that even though the drop says it does contain items for com.apple.mail.email it does provide the items for com.apple.mail.email. Mail dropped from Microsoft Outlook however confirms to the protocols and returns the items for com.apple.mail.email Is this a known issue with Apple Mail and is there are reason for what seems to be a non-compliance to it own standards in Apple Mail?
0
0
280
Dec ’24
ShortCuts: Send email with email address as a parameter
Hello everyone, I would like to dictate a text with Shortcuts and then send it to one of two e-mail addresses (private or business). I would like to be able to select one of two email addresses. Unfortunately, I am not able to pass an email address as a parameter to the Send email function. Is it possible to do this? I'm new to apple and I'm all but not a programmer. So take pity on me :) Best, Niko
0
0
293
Dec ’24
Batching CatalogSearchRequests for Spotify Playlist conversion
Hi! Im attempting to make a spotify to apple playlist converter and to do so am using the Catalog search functionality of the api. My question is, is there anyway to convert these songs to apple music urls (or ids) that does not spam api calls. I know you can batch catalog searches if you have apple music song ids, but I obviously dont have that since the song was shared from spotify. Any ideas/help is appreciated!
0
0
317
Dec ’24