AppleScript

RSS for tag

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

Posts under AppleScript tag

53 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
545
Sep ’23
Using AppleScript, how to confirm a folder action is actually removed?
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?
0
0
421
Sep ’23
Iterate Window instead of Mass Operation
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?
2
0
576
Aug ’23
find path of the wallpaper image in Sonoma
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??
0
1
1.2k
Aug ’23
Remove ‘Fwd: ’ from an forwarded email subject
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
0
0
417
Aug ’23
AppleScript Language Guide. open for access. The parameter is not specified in the syntax.
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
1
0
462
Aug ’23
AppleScript. Split the string into parts by the separator character.
This code does not make sense, I would just like to get information based on it. set MyData to "MyData1|MyData2" on MyScript(MyData) set MyVar1 to "MyData1" set MyVar2 to "MyData2" return MyVar1 & "|" & MyVar2 end MyScript The text is passed in the code: "MyData1|MyData2". I need to divide it into two parts by the "|" symbol. And write each part into two variables: myVar1 and myVar2. How to do it? P.S. The problem is that only one parameter can be passed to on...end. I need to pass two. I'll pass one, then split it into two.
1
0
1.2k
Aug ’23
AppleScript. Tell application "TextEdit" - the syntax of all commands.
Where can I see the entire list of commands for the "TextEdit" program? For example, there is a code: tell application "TextEdit" activate make new document with properties {text:"XDXDXD"} end tell There are two commands in this code: activate make new document with properties {text:"XDXDXD"} Where can I see all the available commands for the "TextEdit" program?
2
0
603
Aug ’23
Automating Split Screen View
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
2
0
1.7k
Aug ’23
Send iMessage AND Text (SMS) from the Command Line
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.
2
0
3.3k
Jul ’23
Mac account detail window [Ventura]
Hello everyone, Up to Monterey, a script we use to make it easier for users to change the password on their local account on the computer has worked very well. But after Monterey, it no longer works. I've searched the web like crazy without finding any solution for Ventura. So I'm giving this form a shot in the hope that someone has a solution or something to help me along the way. The main problem is to go from Users and Groups in System setting to the account detail where you can click the button for changing password. Anyone have any idea? Code looks as follows: use AppleScript version "2.4" -- Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions set userName to short user name of (system info) -- Create a dialog with 3 buttons display dialog "Mac Utility - user: " & userName buttons {"Change Password", "Forget Wi-Fi", "Cancel"} default button 1 with title "User Utilities" with icon caution -- Open Users and Groups if result = {button returned:"Change Password"} then tell application "Finder" to open file "Accounts.prefpane" of (path to system preferences) -- Show account details window -- This is where I'm stuck -- The rest of the script bellow else if.... The earlier code looked as bellow that doesn't work with Ventura. tell application "System Events" tell application process "System Preferences" delay 1 click button -1 of tab group 1 of window 1 end tell end tell Tanks in advance.
3
0
482
Jul ’23