AppleScript

RSS for tag

AppleScript allows users to directly control scriptable Macintosh applications as well as parts of macOS itself.

Posts under AppleScript tag

55 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

Iterate Window in Specified Application
I get the error code: System Events got an error: Can't get application of "Application Name" during this line repeat with currentWindow in every window of application foregroundApp of this code on run tell application "System Events" set foregroundApps to displayed name of (every process whose background only is false) as list repeat with foregroundApp in foregroundApps set appName to foregroundApp as text if exists (window 1 of process appName) then repeat with currentWindow in every window of application foregroundApp end repeat end if end repeat end tell end run I'm guessing I'm missing something like set x to foregroundApp as y repeat with currentWindow in every window of application x How do I get the windows of the application foreground app?
0
0
517
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
Enabling Supervised Mode on an iOS Device
I'm looking to develop an application that can enable the Supervision Mode on an iOS device that is connected over a USB cable. For this, I'm looking to use Apple Configurator's Configuration Utility which has Handlers such as "CNFGPrepareSpecifiedDevices". I'm planning to invoke the " (script library)" and call CNFGPrepareSpecifiedDevices along with the required parameters from my application. This will have a dependency on the Apple Configurator installed on the Mac. Is calling the Configuration Utility scripts and handlers within my application legally allowed by Apple? Or is there any alternative approach someone can suggest?
1
0
502
Sep ’23
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
Sending Email via AppleScript & Mail.app broken
A while back I tried to send an email from my app by calling an apple script. This worked well for a while, but since Apple Mail version 16.0 my message is always sent as "citation". So, in the style as if my original message was a message I replied to. This means that instead of having a message body saying "Hi you", the message generated by Apple Mail is "> Hi you". Does anyone here have similar issues? I filed a bug report (FB11734014) a year ago but it is still in "Open" and I am starting to wonder if AppleScript is still officially supported/taken serious by Apple?! Is there any other, recommended way of how to easily send an email from one's app? Thanks! The apple script that I use: tell application "Mail" set this_message to make new outgoing message at beginning of every outgoing message set content of this_message to "This is the body of the message" tell this_message make new to recipient at beginning of every to recipient ¬ with properties {address:"test@example.com"} end tell set subject of this_message to "The subject" send this_message end tell The email this generates looks like this: --Apple-Mail=_ADD491E8-EDEC-45CB-9F44-EF266087DB84 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii > This is the body of the message --Apple-Mail=_ADD491E8-EDEC-45CB-9F44-EF266087DB84 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=us-ascii <html><head><meta http-equiv="content-type" content="text/html; charset=us-ascii"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class="Apple-Mail-URLShareUserContentTopClass"><br></div><div class="Apple-Mail-URLShareWrapperClass" style="position: relative !important;"><blockquote type="cite" style="border-left-style: none; color: inherit; padding: inherit; margin: inherit;"> This is the body of the message </blockquote></div></body></html> --Apple-Mail=_ADD491E8-EDEC-45CB-9F44-EF266087DB84--
1
1
536
Oct ’23
codesign not signing helper executable in AppleScript bundle
My AppleScript .app bundle contains a helper executable. Table 3 of TN2206 says that executables may be in either Contents/MacOS or Contents/Helpers, but Quinn's first reply in this post says that Contents/MacOS is better. So I put the helper in Contents/MacOS, alongside applet. I sign the AppleScript .app bundle for Developer ID and Hardened Runtime by running the codesign command with arguments recommended by Quinn in this post. Result: Notary Service rejects the .app bundle due to 3 issues with the helper: is not signed with a valid Developer ID certificate does not include a secure timestamp does not have the hardened runtime enabled (Possibly it still has a years-old signature without Developer ID and Hardened Runtime). So it seems that the the helper is not being (re-)signed. If, instead of signing the .app bundle, I run Quinn's codesign comand twice, once on the applet and once on the second executable, then Notary Service is happy with the bundle. I was hoping that, after all these years, codesign is now smart enough to find and sign all of the executables inside a bundle. Both executables are x86_64 non-fat (I guess I should fatten those) and I have installed Xcode 15.1 Beta. Should I file a bug, or am I doing something wrong?
1
0
453
Oct ’23
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
Creating headers in Apple Mail using AppleScript
My grand plan is to have a rule in Apple Mail that automatically adds a custom header to each message. This rule would be last and each of the rules before that last rule would check for the presence of this custom header and therefore no longer work for incoming mail. So the plan is to have a small AppleScript that adds this header but I am running into a block which hopefully someone can help we with. I have the following pieces of code: tell application "Mail" activate set myHeader to make new header with properties {name:"X-MySecretHeader", content:"It's been set"} end The last statement fails with the following error: Mail got an error: Can’t make or move that element into that container. I then changed the code slightly: tell application "Mail" activate set theMessage to get item 1 of (get selection) set theHeaders to headers of theMessage set myHeader to duplicate item -1 of theHeaders end Mail got an error: Headers can not be copied. That last error seems to indicate that I'm trying to do something which cannot be done. I have also tried to do this in a tell block to theMessage but that did not change anything. Anyone has an idea?
0
0
459
Dec ’23
Simple scriptable macOS Xcode Swift App example please
Does anyone have a simple example for a scriptable App in Swift in Xcode for macOS? I mean one that exposes objects and functions to applescript? I've checked all examples that I could find. But for me they get me confused about whether they are accessing the actual and already existing instance of a class or whether they are creating an instance with AppleScript, or just using the class definition to call some function. Also the way they are defined in the .sdef file does not help. It would be great if someone had a really simple example, stripped down to bare minimum that 1) defines a class with a single function, 2) creates an instance of the class inside appDelegate and then 3) the function of the specific instance is called using appleScript. 4) It would be great if the function could do something in the GUI, such as change a text of a Label.
0
0
490
Dec ’23
How can I kill the script/app after issuing the shutdown command?
Hallo, I created an AppleScript for my Synology NAS and saved it as NAS.app. This starts the server and mounts the network drives. If the script is run again when the server is already online, it can shut down the Mac and server. That works very well. But the next time I start the Mac, this app is immediately active again and may have to be closed manually. I'm now looking for a way to prevent this. How can I kill the app after issuing the shutdown command? Here is the part with the Mac shutdown code. set ProcNm_ to name of every application process whose visible is true end tell repeat with i_ from 1 to count items of ProcNm_ set TarProc_ to item i_ of ProcNm_ --display dialog TarProc_ try if TarProc_ is not "Finder" then tell application TarProc_ to quit "without saving" end if end try end repeat try do shell script "sudo /sbin/shutdown -h +1" end try quit I hope somebody can help me. b. r. Binh
0
0
426
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
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
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
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
What APIs can be used to modify the Music.app library?
Hi everyone, I'd like to write a one-off app to make some modifications to my own Music.app (aka iTunes) library. This is for content that I have stored using iTunes Match, so it's effectively a library that dates back to the iTunes years. However, from what I can see, APIs with modification support are essentially... nothing? I can get everything ready in a simple Swift command-line app using https://developer.apple.com/documentation/ituneslibrary, however I can't actually write any changes. It seems that the only modification solution available is actually still AppleScript. Everything there still works, and in theory I could do the majority of the logic using iTunesLibrary and then do the modifications with AppleScript via NSAppleScript. In order to do this, I just need an identifier that I can use to correlate items from each API. There is: iTunesLibrary: https://developer.apple.com/documentation/ituneslibrary/itlibmediaentity/1809728-persistentid AppleScript Item object for AppleScript has a "persistent ID" property which sounds like the same thing There's also an id property However, in my experiments it didn't appear that any of these correlated together Of course, I could possibly re-write the whole thing using AppleScript, but that would be slow and painful! So, posting here to see if anyone has any idea how or even if this can be done. Hope someone can help! Thanks in advance
0
0
367
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
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