AppleScript

RSS for tag

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

Posts under AppleScript tag

64 Posts
Sort by:
Post not yet marked as solved
2 Replies
3.1k Views
This code sends a text message from the command line on run {targetBuddyPhone, targetMessage}   tell application "Messages"     set targetService to 1st service whose service type = SMS      set targetBuddy to buddy targetBuddyPhone of targetService     send targetMessage to targetBuddy   end tell end run  And this code sends an iMessage from the command line on run {targetBuddyPhone, targetMessage}   tell application "Messages"     set targetService to 1st service whose service type = iMessage     set targetBuddy to buddy targetBuddyPhone of targetService     send targetMessage to targetBuddy   end tell end run  I’m looking for a solution to combine this code into a single snippet that will try to send an iMessage if the recipient has iMessages, but if they do not, then the code will send a SMS message.
Posted
by
Post not yet marked as solved
3 Replies
2.2k Views
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
Posted
by
Post not yet marked as solved
1 Replies
1.6k Views
I would like to automate opening two finder windows in split-screen View (the full screen split view you get by hovering over the green full-screen button and selecting Tile Window, not the windows style maximize) If possible preferably via AppleScript but would be okay with Automator or shortcuts. Any help would be greatly appreciated. Running an M1 Air on Monteray. my current workaround is this AppleScript, however, I would like to be using split-screen so it's in its on mission control workspace. PS New to mac and Applescript. tell application "System Events" to set the autohide of the dock preferences to true tell application "Finder" close windows open ("/Users/arthur/" as POSIX file) set screenBounds to bounds of window of desktop set screenWidth to item 3 of screenBounds set screenHeight to item 4 of screenBounds set the position of the front Finder window to {0, 0} set the bounds of the front Finder window to {0, 0, screenWidth * 0.5, screenHeight * 1} make Finder window set the position of the front Finder window to {screenWidth * 0.5, 0} set the bounds of the front Finder window to {screenWidth * 0.5, 0, screenWidth, screenHeight * 1} set the target of Finder window 1 to ("/Users/arthur/Downloads" as POSIX file) activate end tell
Posted
by
Post marked as solved
3 Replies
4.7k Views
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?
Posted
by
Post not yet marked as solved
2 Replies
517 Views
Hi, Recently we tested how to minimize app using Applescript as below picture and find some interesting phenomenon. After executing the script, if “miniaturized” marked in green (recognized as parameter), it failed to set minimize property. If “miniaturized” marked in purple, it is ok to set minimize property. Because the syntax is exactly the same, could you help to guide us what is wrong with the script? Sincerely, Thanks, YM
Posted
by
Post not yet marked as solved
0 Replies
531 Views
Apparently, the call to performDefaultImplementation of an AppleScript is called on the main runloop with the defaultModes only, so it is not called while a menu (or contextual menu) is open. This is very unfortunate as it means the execution of these scripts is delayed until after the menu is closed. Looking for a way to change this behaviour but I have no clue how... Thanks ! Daniel
Posted
by
Post not yet marked as solved
1 Replies
362 Views
I am trying to figure out how to write an AppleScript that will head to a directory, determine if a file with a certain extension exists, and if it does exist, if the file size is greater than a certain size. And run that check every so often. Hung up on how to look at all files in a certain folder, look for that extension and then see if the file size is past my threshold. Ideas? Not too good with AppleScript.
Posted
by
Post not yet marked as solved
0 Replies
530 Views
I can't seem to execute Voiceover command with JXA. The following AppleScript works. tell application "VoiceOver" tell commander to perform command "item chooser" end tell However, the following script in JXA throws a mystery error. vo = Application("VoiceOver") vo.commander.performCommand("item chooser") Error 6: An error occurred. I'd really appreciate any help with this! Thanks so much!
Posted
by
Post marked as solved
1 Replies
1k Views
Hey there! I have a Swift app using SwiftUI, and I want to run some AppleScript Safely inside it. Here is my current code: func example() { var script = NSAppleScript(source: """ tell application "Music" play repeat with vlm from 0 to 100 by 1 set the sound volume to vlm delay \(fadetime / 100) end repeat end tell """) DispatchQueue.global(qos: .background).async { let success = script?.compileAndReturnError(nil) assert(success != nil) print(success) } } However, it does not do anything. I have tried using process, however, that simply errors out in -600 "The application is not running." Is there something I am missing here? Thank you!
Posted
by
Post not yet marked as solved
0 Replies
329 Views
Hey I am new user of MacBook Pro and want to use apple script. I write the script in script editor to delete emails from particular sender. When I click Run this script is running without any error in the Script editor but when I check in the mailbox all mails are still there. Can someone guide me how to use apple scripts. tell application "Mail" set theMessages to every message of inbox whose sender is "***" repeat with theMessage in theMessages set theAccount to account of mailbox of theMessage set mailbox of theMessage to mailbox "Trash" of theAccount end repeat end tell
Posted
by
Post marked as solved
1 Replies
560 Views
Using the highly-rated answer from StackOverflow How to send an imessage text with applescript, only in provided service?: on run {targetBuddyPhone, targetMessage} tell application "Messages" set targetService to 1st service whose service type = iMessage set targetBuddy to buddy targetBuddyPhone of targetService send targetMessage to targetBuddy end tell end run On Ventura, this errors at save with "Expected class name but found identifier." and highlights the first instance of "service". How should the script be corrected? Is there a difference in grammar between releases? This code works on Big Sur.
Posted
by
Post not yet marked as solved
0 Replies
895 Views
I'm specifically talking about macOS because the others are very different beasts. Since, well literally the first release of OS X 10.0, there has not been a coherent automation framework on the Mac. The last OS to offer that was Mac OS 9.X To define terms, by coherent, I mean from the command line environment up through the user interface. A single, coherent automation framework that is usable across the entire OS, ala PowerShell on windows. That doesn't exist for current macOS, nor has it ever existed for any variant of macOS since again, Mac OS 9.X At best it has been a pastiche of completely different frameworks, operating modalities and languages all desperately trying to work together via osascript and do shell script. That any of it works at all is like a talking dog: the fact the dog talks is a miracle, bad grammar is minor. Yes, I know Shortcuts exist, but shortcuts are a developer managed bit of pseudo-automation. If I limit myself to Shortcuts, then I have little to no ability to do anything beyond what the developer of that application chooses to allow me to do. For example, at least as of Ventura, Dictionary, Font Book, iMovie and others have no shortcuts. I can't create my own shortcut for those apps, the devs have to do that. (the fact that Apple is not flooding us with Shortcuts shows how little Apple cares as a company about this. Individual teams wax good and bad, but clearly, Apple doesn't think dogfooding here is important.) (Note: if your only comment is to ask why anyone would care about automating , that's absolutely not the point. The idea is to make it possible for everyone, not just developers, to create in ways no one can predict outside of an application's constraints.) "Well, there's always AppleScript/JXA" I will not write down the laughter here, but there is laughter. As of Ventura, there are no less than 32 Apple applications with no scripting dictionary at all, so neither AppleScript/JXA can work. You might be able to use UI scripting, but no guarantee there, and UI scripting is a fragile thing, at best suited to a last resort option. Again, Apple could lead the way, they choose not to. But that's only the UI levels. Below the UI, then there's no coherency at all. You're stuck with shell scripts and whatever a given utility's author chose to allow, and sharing data and information is no better than at the UI level, and neither the command line level nor the UI level know the other exists. The only way to combine the two is again: Do Shell Script Osascript That is not a coherent automation framework. Then of course, there's the lack of documentation. AppleScript has inherited at least the language guide, which is well-written and usable, but the command line level has man pages. Which are the most inconsistent things. Some utilities' man pages are remarkably useful, others don't even have man pages. At all. Other utilities man pages have syntax errors for the command that brings up the help page and that is all the man page does. Ponder the disconnect there: --help provides detailed usage information that is not in the man page. The man page says use ---help which is bad syntax. This seems odd, but why is the --help output not also in the man page? Is that not the purpose of the man page? Man page inconsistencies aside, Apple developer support has only ever barely acknowledged automation/scripting and really never supported it except for accidentally. Even in a language like AppleScriptObjecttiveC, (ASOC) which one can use for real apps in the MAS, the response from DTS for help was "use the mailing list." (not that DTS is that good anyway, I had an issue with CoreWLAN in ventura, and have since been ghosted on any notification that the OS bug that was causing me problems has been fixed. Maybe it has, maybe it hasn't. I can test for it, but why did DTS ghost an actual SwiftUI issue for an app written in Swift? ¯_(ツ)_/¯ This is a solveable problem, Microsoft has solved it in terms of language, documentation and coherency, with PowerShell. Leaving aside architectural issues that make it almost impossible to properly secure windows, the way MS treats PowerShell is the example. Windows as an OS has excellent support for automation, the documentation for PowerShell is amazing, (the documentation for a scripting language is better than Apple's entire developer docs, which is just inexcusable.) The difference between automating either platform is night and day, and in a race with two horses, Apple is a distant fifth. The language syntax here is almost a non-issue. In fact, I think using a "powershell'd" version of Swift would be an excellent idea. Create a simpler version of swift in the way PowerShell is a simpler version of C#. Make it so if you need to call a framework outside of the main automation framework, you can do so with ease, ala PowerShell. For example, PowerShell has no GUI primitives the way AppleScript does, so to create a file choose browser in PowerShell, you instead invoke the correct .NET framework: Add-Type -AssemblyName System.Windows.Forms $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ InitialDirectory = [Environment]::GetFolderPath('Desktop') } $null = $FileBrowser.ShowDialog() Ironically, you can do this with ASOC, but only for items that have ObjC interfaces, which will be shrinking. Having a coherent automation framework with a primary language that allows people using it to more easily use "full" Swift and its frameworks as needed is a force-multiplier for Apple. It would help people do more with macOS and maybe other platforms depending on implementation, not less. It would not force people to beg the developers of their apps to write Yet Another Shortcut to do this thing that they can't do, (or have to do the dance to convince the dev that their need is worth the trouble), and it would allow people to solve their problems in their own ways for their own needs. But, it requires Apple to care about this at all, and that is where the problem is. This is fixable, Apple has the resources to fix it, what they lack is interest and desire. But creating and fully supporting a proper automation framework would add so much to macOS and the other platforms by extension that there's no logic behind not doing so. Maybe next year. (in the best lol of all, you can't even create an automation tag for a post. sigh.)
Posted
by
Post not yet marked as solved
0 Replies
550 Views
AppleScript code: tell process "EdgeMonitor" tell menu bar 2 UI elements end tell end tell When trying to access UI elements for a given application, we are getting different results on macOS Ventura and macOS Big Sur for the same app and same script. We are specifically trying to access the popover UI element which is accessible in Ventura but not in Big Sur. Can someone please help here to check why this might be happening? Thank you UI elements in macOS Ventura: menu bar item 1 of menu bar 2 of application process "EdgeMonitor" of application "System Events", pop over 1 of menu bar 2 of application process "EdgeMonitor" of application "System Events" UI elements in macOS Big Sur: menu bar item 1 of menu bar 2 of application process "EdgeMonitor" of application "System Events", menu bar item 2 of menu bar 2 of application process "EdgeMonitor" of application "System Events"
Posted
by
Post marked as solved
5 Replies
1.1k Views
Hello. How in AppleScript to get a list of all files in all subfolders without any conditions? I understand I need to use find. What the code will look like without any conditions, I need to get absolutely all the files: no matter the extension, no matter the file name. The only thing I would like is not to receive hidden files.
Posted
by
Post marked as solved
3 Replies
1.3k Views
Hi Everyone, I'm new to mac's and AppleScript so I apologise in advance for the level of questioning/quality of code. I've been trying to create an applescript to go through every menuitem and list out the keyboard shortcuts. I've created a script to add keyboard shortcuts, but have only done so as global shortcuts. Some of them don't work in different apps so I'm trying to create a verbose list that has every menuitem for every application so I can populate my script, then eventually have application specific shortcuts for every menuitem that has a shortcut currently. Below I'm trying to generate that list of current shortcuts but I'm stuck on the syntax of getting the properties of attributes of items and displaying them. The below is where I have got to: set allMenus to {} set everything to {} set onlyEnabled to {} tell application "System Events" to tell process appName set allMenus to name of every menu of menu bar 1 end tell global testCounter global currentCounter set testCounter to 14 set currentCounter to 0 repeat with menuName in allMenus set the end of everything to strings of getAppMenuItems(appName, menuName, onlyEnabled) if currentCounter > testCounter then log "EXIT REPEAT!!!!!!!!!!!!!!!" exit repeat end if end repeat on getAppMenuItems(appProcess, appMenus, enabledItems) tell application "System Events" to tell process appProcess # Get all menu items set theItems to every menu item of menu appMenus of menu bar 1 log "BREAKAKKKKKKBREAKKKKKK" set appMenuName to name of menu appMenus of menu bar 1 --log appname repeat with theMenuItem in theItems set currentCounter to currentCounter + 1 if currentCounter > testCounter then exit repeat end if set itemAttrib to get every attribute of theMenuItem repeat with aAttrib in itemAttrib if the name of the aAttrib is "AXMenuItemCmdChar" then set props to get properties of attribute "AXMenuItemCmdChar" of every menu item of menu appMenus of menu bar 1 repeat with aProps in props if the value of aProps as text is not "missing value" then log (appMenuName & "->" & name of theMenuItem as text & "->" & value of aProps as text) end if end repeat log (appMenuName & "->" & name of theMenuItem as text & "BREAKAKKKKKKBREAKKKKKK") end if end repeat end repeat return name of every menu item of menu appMenus of menu bar 1 end tell end getAppMenuItems (* tell application "System Events" get name of menu item 2 of menu 3 of menu bar 1 of process "Finder" --> "New Folder" get every attribute of menu item 2 of menu 3 of menu bar 1 of process "Finder" --> {attribute "AXRole" of menu item "New Finder Window" of menu "File" of menu bar item "File" of menu bar 1 of application process "Finder", [...] get properties of attribute "AXMenuItemCmdChar" of [...] --> {value:"N", class:attribute, settable:false, name:"AXMenuItemCmdChar"} get properties of attribute "AXMenuItemCmdModifiers" of [...] --> {value:1, class:attribute, settable:false, name:"AXMenuItemCmdModifiers"} *) Currently this outputs: Apple->About This Mac->Q Apple->About This Mac->Q Apple->About This MacBREAKAKKKKKKBREAKKKKKK Apple->System Information…->Q Apple->System Information…->Q Apple->System Information…->Q From the output I have surmised that when I'm logging the output (log (appMenuName & "->" & name of theMenuItem as text & "->" & value of aProps as text) The properties aren't actually the properties of theMenuItem attribute, but that it has found 3 AXMenuItemCmdChar in the full Apple menu, and is just putting those 3 against each theMenuItem. I've tried a few ways around this, mainly changing set props to get properties of attribute AXMenuItemCmdChar of every menu item of menu appMenus of menu bar 1 to set props to get properties of itemAttrib or set props to get properties of attribute "AXMenuItemCmdChar" of itemAttrib but can't seem to find the right way of getting it to work. Furthermore, how I would correctly reference the parent within my log() statement (I can see the syntax for referencing the child (e.g. x of y of z) but how do you go the other way? Thanks in advance
Posted
by
Post not yet marked as solved
0 Replies
685 Views
Hi! I am trying to set up some tests on AppleTV and i figured the way to do them is by using airplay video. The issue here is i want the tests to be automatically executed and i don't seem to see any way to automatize airplay except for using applescript with quicktime player. I tried to write a script that could do that but i get the "wrong index" error. My code below: to replaceFrontQTPlayerWithFile(aFile) tell application "QuickTime Player" activate try set frontDoc to front document close front document on error err number errNum if errNum is -1719 then -- There is no open document else if errNum is -10000 then -- Front doc exists, but does not really... else log err end if end try open aFile tell application "System Events" to tell process "QuickTime Player" tell window 1 click button 4 delay 1 pick menu item "WRO-CT-IE-ATV-2" of menu 0 of button 4 end tell end tell play front document -- Hide QTP #tell application "System Events" # keystroke "h" using command down #end tell end tell end replaceFrontQTPlayerWithFile on run set unixFile to "/Users/aet/signals/roomba_sdr_vfr_no_audio.mov" set macFile to POSIX file unixFile set fileRef to (macFile as alias) my replaceFrontQTPlayerWithFile(fileRef) end run Here i have two questions: Is there any other way to do that (maybe some airplay API/command line tool that could help me)? If not is there a way to make my code work? I am using macOS 12 device with tvOS 16 AppleTV
Posted
by
Post not yet marked as solved
0 Replies
433 Views
Recently we tested how to move app using Applescript as below picture and find some interesting phenomenon. The following coordinates are meant for the second screen, but only FaceTime cannot be moved to the second screen. It can only be moved within its own screen, while other apps can be smoothly moved to the second screen. Because the syntax is exactly the same, could you help to guide us what is wrong with the script? Sincerely, Thanks, Bruce
Posted
by