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

Apple script stopped working - "invalid index"
Hi there, I've created an Automator app running an Apple script to toggle a setting within the "systems preferences". I'm on German systems settings, so I hope you can make any sense out of the below... The intention is to toggle a setting within "Desktop & Dock" off and on again (i.e., just toggle the blue switch left and right in one go). I've attached screenshots to make clear what I mean. I've been running the following Apple Script (for only a few weeks so far), and it worked perfectly fine until just recently: on run {} repeat 2 times tell application "System Settings" to activate tell application "System Events" tell process "System Settings" click menu item "Schreibtisch & Dock" of menu "Darstellung" of menu bar 1 delay 2 tell window "Schreibtisch & Dock" click checkbox "Beim Programmwechsel Space auswählen, der geöffnete Fenster des Programms enthält" of group 9 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 end tell end tell end tell end repeat end run Now, since a few days, it keeps throwing an error message as follows: "System Events" has received an error: "group 1 of window "Desktop & Dock" of process "System Settings"" cannot be read. Invalid index. I'm not sure whether I've made an update of the MacOS in the meantime or what the reason could be. As mentioned, I'm not using the script for too much time, only a few weeks so far (mid-February). My MacOS version: Sonoma 14.3.1 Macbook Pro 16" 2021 (Apple M1 Pro) Unfortunately, my research did not point me into the right direction, I couldn't find an answer as to what exactly I'd need to change. Would much appreciate if anyone can help me set the correct index, or how to otherwise correct the script. Thanks a lot! S.
0
0
370
Mar ’24
Failed to gather url on browser windows via osascript (Apple Script)
I've been using Apple Script below for gathering every title & url of opening tab from browser window. It's been works fine for a few years but it's failed recently (maybe in last two weeks). #!/usr/bin/env osascript -l JavaScript // url.js function run(arg) { arg = arg.toString() let browser = '' switch(arg) { case 'chrome': browser = 'Google Chrome' break; case 'edge': browser = "Microsoft Edge" break; default: browser = 'Google Chrome' break; } Application(browser).windows().forEach((window) => { console.log('\n\n') window.tabs().forEach((tab) => { const url = tab.url() const name = tab.name() console.log(`${name}\t${url}`) }) }) } It's been works like below (imagine that you open https://example.com on edge) $ url.js edge example.com https://example.com But I found it failed today. $ url.js edge execution error: Error: Error: Application isn't running. (-600) of course I'm make sure Edge browser up and running. Same error for Chrome. I keep my environments(os, browser etc) up-to-date but I don't know which updates affects here. Any thoughts or helps are welcome. Settings MacBook Pro M2 Sonoma 14.2.1 Edge 122 Chrome 122
0
0
343
Mar ’24
Copy all Photos and Videos in a folder created after a certain date to a new folder
Hello - I have a lot of photos and videos that end up in my downloads folder from amusement park photographers and rides. The problem is that when I go to download a recent trip the amusement park website downloads everything all over again, and creates a lot of folder and files in a random sequence. So I have to go through each folder and find the "new" files that I want to add to the Photos app. Could you please help me write a script that would search through the entire downloads folder, and copy all of the files that are of photo or video type, created on or after a specified date, into a new folder within the downloads folder? This is effectively just eliminating the subfolders that the website download process created. From there I would go to the newly created folder and sort by created date to obtain the new photos I want to add to the Photos app.
1
0
440
Feb ’24
Seeking Assistance with AppleScript for Apple Reminders to Increment Date Without Changing Time to Midnight
Hi Community, I'm in need of some assistance with an AppleScript that increments the due date of reminders in the Apple Reminders app by one day. The reminders are set with a due date but do not have a specific time associated with them; they are just set to occur at some point during the day. The challenge I'm facing is that when I use AppleScript to add one day to the due date, the script is setting the new due date with a time of midnight. Since the original reminders do not have a time (just a date), I want the script to increment the date without adding a time. Here is the script I'm currently using: set myList to list "My List" repeat with myReminder in reminders of myList if (due date of myReminder is not missing value) then set currentDueDate to due date of myReminder set newDueDate to currentDueDate + (1 * days) set due date of myReminder to newDueDate end if end repeat end tell I am looking for a way to preserve the 'date only' attribute of the reminder when adding a day, so it does not default to a time of 00:00. Does anyone have experience with this, or can anyone provide guidance on how to accomplish this? I haven't found a way to specify 'no time' or 'all-day' in AppleScript for the Reminders app. Any help or pointers would be greatly appreciated. Thank you in advance!
1
0
336
Feb ’24
App Automatically Deleted from Applications Folder - OSX
I made an application in Script Editor and it works as expected. But the app seems to be getting automatically deleted at random times. For example, I made it a few days ago, tested it successfully, then went back today to look for it and it was gone. Tested this multiple times. I bit more detail about my process: I wrote the app in Script editor, exported it as an Application with run-only checked and no code signing after manipulating a few things (.plist file, .icns file), I then remove extended attributes and code-sign using terminal. I have an Apple developer account that I use to code-sign: xattr -cr <path_to_app> codesign -s <my_developer_account> <path_to_app_bundle> then I copy the app into my Applications folder and test it successfully a day or more later, the app is gone (and I haven't even opened it again) Ventura 13.1, Mac Book Pro 2021
1
0
444
Feb ’24
AppleScript to Get Open TextEdit RTF or RTFD File Properties & Copy to Clipboard?
A TextEdit RTD or RTFD document will save various document properties that you access and edit by selecting: File > Show Properties > Is is possible to retrieve these properties on a currently open document via AppleScript and place them in the clipboard? I am interested in storing some text in the Comments and later being able to copy it to the clipboard without having to manually open the properties window, selecting the text and copying it.
1
0
379
Jan ’24
Applescript not triggered by mail rule
Hi!! I tried posting about my issue on the macscripter forum, but haven't receieved any love there so I thought I would try giving it a go here. I'm trying to help automate my wife's business which uses mac and am having difficulty getting an applescript to trigger by mail rule. I am not an apple guy and myself use linux, so maybe i am overlooking something basic that I need to enable? I gave the mail.app full disk access in the security panel. If I set a rule, like to move a message to another mail folder it executes without problems. However, I can't get any applescripts to run triggered by mail rule, even something very simple...like writing to the display or logging an event. I looked through the console and saw no error messages or any evidence even that the applescript tried to run. We are running the latest mac sonoma. I asked chatgpt for help trying to debug what was wrong but it ran out of suggestions for me as well. This problem is driving me crazy, I can't understand how a nice new mac computer I bought for my wife's business is failing at the most basic task... arrrgh. Any help greatly appreciated!!!
1
0
380
Jan ’24
Error in signing app from Automator
Hi, I have an app generated by using osacompile on an applescript file. The app works fine as expected. However, when I try to sign it, I get two errors as in the screen shot below: After some googling around, I deleted the _CodeSignature folder in the .app directory but still signing fails with the same error. So, I would like to know two things: Is it possible to sign .app files created using osacompile as in my case? If yes, what am I missing and how to resolve my situation. Thanks,
1
0
394
Jan ’24
How can I run scripts with Sandbox enabled for AppStore distribution ?
Hey! Im new here and currently learning iOS/macOs development (SwiftUI), so...take me easy :) I want to create a simple macOS app to let user set time until computer power off. I found an example with AppleScript and use it on my app, but I found that App won't run with Sandbox enabled, and to deploy app on AppStore it show me that Sandbox must be enabled. The script I want to use: 'tell application "System Events" to shut down' I found some examples that add script onAbsolute path, but after I do that, it won't let me to distribute the app, only export to run local. It is any way to make script running (no matter, if app ask for user permission/admin pass) ?
1
0
625
Jan ’24
Launchctl agent errors
I am trying to automate a backup terminal script to mirror some directories to my NAS. The terminal script is working fine, so I want to automate them. The recommended path uses the launchctl agent, which, from the description, should be what I need. However, I have been running into two errors and looking for answers. I am using a M1 Mac mini with 8GB of memory (for reference). First, I get an error 23, which means too many files are open. If I reboot the machine, this error goes away. But why am I getting this message? I can run the script manually without a problem, and the system is running fine. The machine does little, so it should have almost nothing open. The second error, once the 23 is cleared, is error 12, cannot allocate memory. The machine is an 8GB machine but is reporting 3GB free. Again, the script runs fine in the terminal. I suspect the problem is the agent, but I am unsure how to diagnose or resolve these issues. I do not want to reboot the machine to run the backup, and so is the agent running under some system constraints that are too limiting. Can I change those limits for the job and make the recommended process to automate work? Any suggestions would be appreciated.
2
0
391
Jan ’24
Scripting Zoom App - A Work In Progress
I know that zoom.us is not technically scriptable with Applescript but I have gotten so tired of repeatedly doing the same things when I set up a Zoom session that I started trying to automate the process using System Events and tell process. I was surprised by how much I could automate but then ran into a couple of brick walls. If anyone else is interested in this I'd like to brainstorm on the forum to see how much is possible. Here's some scripting steps I have been playing with. Part of what I decided to do in order to simplify the data entry involved in scheduling a new meeting and sending an email to the client was to run everything from Filemaker. The variables shows as $theMonth, $theDay, etc are being set in FileMaker before the Perform Applescript step so this is not a viable Applescript. You could use three separate Applescript dialog boxes to get the name date and time. The key code 48 is a tab to move between fields on the zoom interface. Key code 76 is the enter key which will OK the automatic entries in the Calendar so that I can quit Calendar and get back to zoom. There seems to be no way to stop zoom from automatically posting a new session in the Calendar. tell application "zoom.us" to activate tell application "System Events" tell process "zoom.us" repeat until window "Schedule Meeting" exists click menu item "Schedule Meeting..." of menu "Zoom.us" of menu bar 1 delay 0.2 end repeat set topicField to text field 1 of "Schedule Meeting" keystroke "Whatever You Want It To Be"" key code 48 keystroke $theMonth as text key code 48 keystroke $theDay as text key code 48 keystroke $theYear as text key code 48 keystroke $theHour as text key code 48 keystroke $theMinute as text key code 48 keystroke $theMonth as text key code 48 keystroke $theDay as text key code 48 keystroke $theYear as text key code 48 keystroke ($theHour + 1) as text key code 48 keystroke $theMinute as text Delay 2 key code 36 key code 76 key code 76 end tell end tell tell application "Calendar" to quit tell application "zoom.us" to activate tell application "System Events" tell process "zoom.us" repeat until window "Schedule Meeting" exists click menu item "Schedule Meeting..." of menu "Zoom.us" of menu bar 1 delay 0.2 end repeat select button ?????? end tell end tell The zoom.us interface for Meetings has what appear to be buttons for each scheduled meeting. I am trying to figure out how to select the right button so that I can then copy the invitation to put in an email. Tabbing does not let me select the button. If anyone has any ideas, I'd love to hear them, and if my further experiments produce any results, I'll post them. Thanks
3
0
2.4k
May ’21
Access of app to read and write - Administrator cannot change access
Hey, please take a look at this code and tell me why it does not want to save file. Script is giving me an error that I do not have access to file. Error suggest that I need to change the mail access to read and write. After I open mail app -> information, previously unlocking the lock, I cannot add access, cannot change it and Sonoma showing error that I cannot change setting since I do not have access. Mail app has full disc access, as well as script editor and terminal. set acc_name to "TEST" set sender_email to "andrew" set email_subject to "Test" set download_folder to (POSIX file "location") as string set python_script to "location2" using terms from application "Mail" tell application "Mail" if not it is running then activate repeat with aMessage in messages of mailbox "Alpha" of account acc_name set client_sender to sender of aMessage set client_subject to subject of aMessage set client_attachment to name of mail attachment of aMessage as rich text set attachment_file to mail attachment of aMessage if client_sender is sender_email and client_subject is email_subject then --set noti_text of subject of aMessage display notification (client_sender & " " & client_attachment) as rich text with title client_subject tell application "Finder" delete (every item of folder download_folder) end tell --set attachmentPath to open for access download_folder & client_attachment with write permission --close access attachmentPath 6 save attachment_file in download_folder end if end repeat end tell end using terms from I am starting to think that it might be system error, not script issue.
2
1
396
Oct ’23
Automator creation of file names in 3rd party exports
Firstly, I'm a novice in automator & applescript. I have however managed to create an automator workflow that falls over at the second iteration. What I am trying to do is the following: working in Sierra I have an old app (Discus) that has an export capability for graphics that reside in the app. It appears that Discus does not have either AppleScript or Automator built in. The workflow has the following steps: move cursor direction right one keystroke (working) / select export from the menu (working) / keystroke a unique file name for the export file (not working) / save to folder (working) / Loop x1000 (working). At the moment the workflow falls over the second time it attempts to name the export file. I cannot find a way to auto create a new name for each iteration. Consequently Discus hangs waiting for a decision on whether to overwrite the initial exported file or to create a new filename. It would be beneficial to create a new file names using sequential numbering. There is an action for doing this in the finder, however this does not work in the workflow as I presume the file saving action is taking place in Discus, not in the finder. Any ideas on how to overcome this, or evenif it's possible. It may be that Automator in Sierra is more limited in capability than in current Mac OS? Thanks
0
0
390
Oct ’23
Constant "Your session has expired" (error 1100)
Trying to do a CLI build with XCode (using NativeScript).Was working fine a couple days ago. However, now I cannot get past this error when trying to build:DTDeviceKit: deviceType from 7c64d8a014fd573ca3623ef80b158e57e7aea7c5 was NULL2018-05-07 11:57:54.696 xcodebuild[14808:865317] DVTPortal: Service '&lt;DVTPortalViewDeveloperService: 0x7fe4e17686d0; action='viewDeveloper'&gt;' encountered an unexpected result code from the portal ('1100')2018-05-07 11:57:54.696 xcodebuild[14808:865317] DVTPortal: Error:Error Domain=DVTPortalServiceErrorDomain Code=1100 "Your session has expired. Please log in." UserInfo={payload=&lt;CFBasicHash 0x7fe4e242ed70 [0x7fff9115faf0]&gt;Whenever I open XCode and to go preferences-&gt;account, my team account already reports "Session has expired". I log in successfully, then close Xcode, then login again, and once again it says the "Session has expired".Enabled two factor authentication hasn't helped. I get the identical error from two separate machines (Mac Mini and MacBook Air). Using latest High Sierra OS and XCode.My developer accounts logs in fine to the developer portal on Apple's Developer site, iTunes connect, etc..
15
0
35k
May ’18
Applescript to estimate driving times
I'm trying to automate making accurate alarms to help me estimate when I need to leave to be on time for meetings. To do this accurately, I want to access Apple Maps (or Google Maps services, or ...) to get an estimate of driving times given current driving conditions. So I'd like to query the maps service reasonably close to departure time. (If I get an estimate on Sunday evening at for an appoint on Monday at rush hour leaving from a different location, it's obviously going to be inaccurate.) Ideally, I'd like to directly access my Apple Calendar to get the appointments and their locations. But if that's too complicated, I'll just create a formatted text file or sqlite file or whatever with the information. I've considered doing this with Shortcuts, Automator, AppleScript, JXA, Hammerspoon, Python, etc. It seems that each technology has part of what I need, but not all. But I don't have deep knowledge of any of them. Perhaps someone can advise me on the most appropriate technology. Here are my impressions: Shortcuts: Has built in access to driving time estimates and Calendar events, but awkward and possibly too limited in terms of program logic. Automator: Is more capable than Shortcuts in some ways, less in others. AppleScript: I don't know. JXA: I prefer Javascript to AppleScript, but it seems hard to find good documentation. Python: I'm quite proficient in Python, but I don't know if I can access things like Calendar events. If not, I'm willing to keep a separate file manually. (I could perhaps create the Calendar events from this, except that some appointments are set up by my work.) Hammerspoon: I don't have any experience with it and don't know it's capabilities. Any advice appreciated.
2
0
579
Sep ’23
AppleScript: RemoveEmojisFromFilename
Hi, So I've been trying to set up this applescript which removes Emojis from filenames in my Download folder. Right now it's set up to work with a droplet, but for some reason I get an error message when running the script. This is my error message: Can’t get name of alias "Macintosh HD:Users::Downloads:Armandocolor IDREAM - Alone RMX BURNAxBOY ❤️❤️❤️❤️❤️❤️.mp3". This is my scriptcode: -- Prompt user to choose files set selectedFiles to choose file with prompt "Select files to remove emojis from:" with multiple selections allowed processFiles(selectedFiles) end run on open theFiles -- Called when files are dropped onto the droplet processFiles(theFiles) end open on processFiles(fileList) repeat with aFile in fileList set fileName to name of aFile set newName to removeEmojisFromText(fileName) -- Check if the filename changed if newName is not equal to fileName then set name of aFile to newName end if end repeat end processFiles on removeEmojisFromText(inputText) set outputText to "" repeat with i from 1 to count characters of inputText set thisChar to character i of inputText if (thisChar is not in {"", return, tab}) then try set outputText to outputText & thisChar end try end if end repeat return outputText end removeEmojisFromText Can someone help me out with this code? I'm very much a noob still at applescripting...
0
0
389
Sep ’23
Speeding Up Code
I'm made a script that iterates through all windows and restores minimized windows. However, the script is quite slow. I'm wondering how to speed it up. I believe part of why the code is so slow is that it's looking at every process instead of every foreground process. Is it possible to do something like "repeat with p in every foreground process" or speed up this script through other means? on run {input, parameters} set windowNames to {} tell application "System Events" repeat with p in every process repeat with w in every window of p if value of attribute "AXMinimized" of w is true then set value of attribute "AXMinimized" of w to false end if end repeat end repeat end tell end run
2
0
515
Aug ’23