Post not yet marked as solved
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.
Post not yet marked as solved
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?
Post not yet marked as solved
Hi.
Is it possible to get all Safari/Chrome open tabs with AppleScript? Is it also possible to check if tab audio is enable/play video/music?
macOS
Thank you!
Post not yet marked as solved
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
Post not yet marked as solved
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?
Post not yet marked as solved
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.
Post not yet marked as solved
Im trying to create an apple script to do a mouse click at a certain point on my 2nd monitor. I can get it to work on my main monitor, but not my 2nd monitor. Does anyone know how to force the apple script to work on my second monitor ???
So I am using shortcuts app on MacOS Monterey. When try to run an AppleScript with Keystroke command in it I get an error message that says
"System Events got an error: Shortcuts is not allowed to send keystrokes."
Even I gave all the permission to Shortcuts app in privacy settings.
Is there any other way to do that in Shortcuts app?
Post not yet marked as solved
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--
Post not yet marked as solved
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.
Post not yet marked as solved
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?
Post not yet marked as solved
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...
Post not yet marked as solved
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
Post not yet marked as solved
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?
Post not yet marked as solved
I'm writing AppleScript to make a new folder action and attach it to a specified folder. In case there's already a folder action attached to the folder before make new folder action is executed, I want to delete that folder action already exists.
tell application "System Events"
repeat with i from 1 to count of folder actions
if (path of folder action i) as string is equal to "/Users/foo/MyFolder" then
delete folder action i
exit repeat
end if
end repeat
make new folder action at end of folder actions with properties {name:"MyFolderAction", path:"/Users/foo/MyFolder"}
end tell
However, it is still often that when make new folder action is executed, an error occured saying that "There is already a folder action with this path: /Users/foo/MyFolder".
Maybe it's because that when make new folder action is executed, the delete folder action has not completed yet. Adding a delay 1 before make new folder action makes the error occured less fruequently, but not disappeared.
Is there any method to confirm that the folder action is really deleted before make new folder action is executed?
I'm trying to modify this code to work iteratively:
on run {input, parameters}
tell application id "com.apple.systemevents" to set the value of
attribute "AXMinimized" of every window of every process
to false
end run
Something along the lines of :
on run {input, parameters}
foreach windows of every process
tell application id "com.apple.systemevents" to set the value of
attribute "AXMinimized"
to false
end run
Is this possible?
Post not yet marked as solved
REFERENCE old thread
Find path of the wallpaper image - Apple Community
previous to SONOMA you had a few options to locate the currently displayed desktop wallpaper. you can change the DOCK preference to enable the option to display the wallpaper path on the desktop
[defaults write com.apple.dock desktop-picture-show-debug-text -bool TRUE;killall Dock]
but this does not seem to be working anymore.
you were also able to run a AppleScript to locate and display the wallpaper file. this was done by querying the desktoppicture.db. but it does not appear that SONOMA utilizes this file anymore.
-- Usage: osascript find_wall_pic n
-- n = 1 or 2 (monitor ID)
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
on run argv
if (count of argv) < 1 then
set screenId to 1
else
set screenId to item 1 of argv as integer
end if
if screenId ≤ 1 then
set screenId to 1 -- 1st monior index
else
set screenId to 18 -- 2nd monitor index
end if
set posixaliaspath to (do shell script "/usr/bin/sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"select d1.value || '/' || d2.value from preferences pf1 join data d1 on pf1.data_id=d1.rowid join preferences pf2 on pf1.picture_id=pf2.picture_id join data d2 on pf2.data_id=d2.rowid where pf1.key=10 and pf2.key=16 and pf1.picture_id=" & screenId & "\"") as string
set homepath to (do shell script "echo $HOME")
-- replace "~" in the path to actual $HOME dir
set posixaliaspath to replace_chars(posixaliaspath, "~", homepath)
set aliaspath to (POSIX file posixaliaspath) as string
set posixpath to POSIX path of aliaspath
set imgfile to POSIX file posixpath
-- tell application "Finder" to reveal imgfile
tell application "Finder"
activate
reveal imgfile
end tell
end run
I am looking for a solution similar to the AppleScript above, that can be run on demand to locate the file active on the desktop at that time. any ideas??
Post not yet marked as solved
All works well, except that 'Fwd:' in the subject line, which I'd like to remove when forwarding
tell application "Mail"
set theMessage to item 1 of (get selection)
set forwardedMessage to forward theMessage without opening window
tell forwardedMessage
make new to recipient at end of to recipients with properties {address:"a@b.com"}
set message signature of forwardedMessage to signature "Blank" of application "Mail"
set stringLength to (count subject of forwardedMessage)
set subject of forwardedMessage to (characters 0 thru stringLength) as string
send
end tell
end tell
Post not yet marked as solved
See the screenshots. First, the item "Commands Reference" is on the first level. I click on the arrow, the item "Commands Reference" goes to the second level. I click on the arrow again, the item "Commands Reference" remains in the second level.
Post not yet marked as solved
Here is the help for the "open for access" command. In the Syntax - Parameters section, the first parameter is not specified, but immediately write something in parentheses. In the "write" command, it is done differently.
https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW31