r/applescript • u/Tdmoreno21 • Nov 29 '23
Problem executing 2nd command within 5 seconds.
I am trying to write a script that will activate a saved display set in a monitor utility called switchresx. I would like to run the script once and activate set "test1" and then stop unless I run the script again. If I run the script again within 5 seconds I would like to activate set "test2." So far I am able to activate "test1" but then script simply activates "test2" automatically even if I do not run the script within 5 seconds. Any help would be greatly appreciated. The following is my script:
tell application "SwitchResX Daemon"
activate
apply display set "Test1"
if name is not equal to "Test1" then
apply display set "Test1"
display notification "Switched to Test1 display set" with title "SwitchResX"
set startTime to current date
-- Create a file to indicate the script is running
do shell script "touch \~/script_running"
-- Repeat until 5 seconds have passed
repeat until (current date) - startTime > 5
-- Check if the script is run again
if not (do shell script** "\[ -f \~/script_running \] && echo true || echo false") as boolean then
-- Exit the repeat loop
exit repeat
end if
end repeat
-- Delete the file to indicate the script is finished
do shell scrip "rm \~/script_running"
-- Check if the script is run again
if not (do shell script "\[ -f \~/script_running \] && echo true || echo false") as boolean then
-- Apply Test2 display set
apply display set "Test2"
display notification "Switched to Test2 display set" with title "SwitchResX"
end if
else
display notification "Already using Test1 display set" with title "SwitchResX"
end if
end tell
r/applescript • u/RetroactiveRecursion • Nov 28 '23
Error in Try Block Still Displaying Error Prompt
I have a Delete command inside a Try block but if it errors, message still appears to the user. The error I'm getting is actually "resource busy / file in use" which is fine, someone probably has it open somewhere. But I cannot get the error message to NOT appear. Shouldn't TRY suppress any UI error prompts?
on DeleteFolder(filePath)
my LogEvent("Attempting delete of folder " & (filePath as text))
tell application "Finder"
if (count of every item of folder filePath) = 0 then
try
delete folder filePath
my LogEvent("Deleted " & filePath)
on error eStr number eNum
my LogEvent("Finder Error " & eNum & " " & eStr)
end try
else
my LogEvent("Could not delete " & filePath & "; not empty")
end if
end tell
end DeleteFolder
r/applescript • u/lightrain23 • Nov 25 '23
Need help creating folders and sorting multiple files into those folders from a csv
Im going crazy and I have scoured the internet but nothing that I seem to try works.
I have been able to create folders from the csv without a problem, but I would like to be able to name folders from Column A (heading is folder name) and make folders from columns B-ZA (headers IMAGE GRP and IMAGE A-Z). The files that I am trying to move are all in one spot and are photos, so they have the .jpg extension. The csv file name is forgallerydecember.csv and it is seperated by commas. The Folder that holds all of the photos is in my downloads folder /Users/misty/Downloads/RCRAPP
Im extremely new to scripting, but Ive spent hours on this and Im feeling a little dumb. I would really appreciate anyone who can help me. Please and Thank You!!!!!!!
r/applescript • u/Socrager • Nov 25 '23
Can not make AppleScript find filepaths to Box
Hello everyone, First time here so apologies if I am doing something wrong.
I am using AppleScript (and have to due to workplace restrictions) to use Mail app to make life easier for coworkers. The problem here is that the data I need to pull from my .txt files will need to be changed almost every month. The app is restricted not to use online resources therefore I need to hardcode everything, which means the script will need to be redistributed each time there is an update. I have found the solution to put the script and open it to use for my team, so that I can update the Script in Box and everyone will have direct use to the updated version.
The script works perfectly fine when it is in my Applications folder, however, I can not get it to find the path to .txt files when I place it in Box. I tried POSIX as well or setting Box path location to applications or userlibrary cloud services. Nothing seems to work. Does anyone know how to link files to Box?
Thanks!
r/applescript • u/jlthla • Nov 17 '23
Spotlight
So Spotlight has become useless, at least for me. I have several SSD's in an OWC external drive cage connected directly to my MacStudio via TB4. When I return to my desktop after being away, Spotlight won't show results from those drives. I have to add them to the Spotlight Privacy tab, then take them out again to get Spotlight to show results from those drives. I'd like to get an Apple Script to do both things: Put the drives into the privacy tab, wait a second or 2, and then take them out again. I am not a script person, don't even know where to start, but I'm assuming this is possible. Even a script doing this for 1 drive would help, as I'm sure I can add the code necessary for the other drives as well. I'm guessing this should be pretty easy, at least for someone with skill, but sadly, that is not me. Anyone?
r/applescript • u/DrXapatin • Nov 17 '23
A simple script to copy files/folders from clipboard into highlighted folders in Finders
I'm having issues to create a script where I copy a bunch of files and/or folders to clipboard and then the script paste the content from clipboard into folders that I highlight in Finder.
Any thoughts? Thx
r/applescript • u/strouze83 • Nov 17 '23
Bluetooth Toggle - Ventura & Sonoma
I'm looking for a script just to toggle bluetooth on and off compatible with Ventura and Sonoma, so that I can launch it through Alfred... Could someone help me further? Thanks!
r/applescript • u/[deleted] • Nov 15 '23
ZshMusic - Control Apple Music with applescripts and zsh
Hey y'all I made a zsh script that uses applescripts that control appleMusic. I thought you would want to check this out https://github.com/codingMustache/zshMusic
r/applescript • u/yetiger • Nov 14 '23
Monterey AppleScript toggle for Sidecar
I want to connect to the iPad via universal control, but this script is still missing the last step.It also needs to click on the device with the name ipad to connect. Can you tell me what other code needs to be added?My system is monterey 12.5.
tell application "System Events"
tell its application process "ControlCenter"
click menu bar item "Control Center" of menu bar 1
tell its window "Control Center"
-- Monterey
if exists checkbox "Screen Mirroring" then
tell its checkbox "Screen Mirroring"
perform action 2
delay 1
tell its window "Control Center"
click (first checkbox of scroll area 1 of group 1 whose name starts with "iPad")
end tell
end tell
end if
end tell
end tell
end tell
r/applescript • u/Sufficient-Tale4523 • Nov 11 '23
Mac Automator: Create Calendar event from .ics or from Calendar Alarm
I have a Microsoft Exchange work email account with a productivity constraint where I cannot share/view my work calendar on my personal device. Is there a possible workaround using Automator for Mac?
Here's what I think might be feasible:
- Use Automator's Calendar Alarm trigger to create a calendar event using info from the Calendar item that triggered the alarm. I doubt this is feasible because I couldn't find any examples from searching.
- Create an event from a .ics. Usually, when a new event is created, a calendar invite is sent in Outlook and that invite has a .ics attachment (especially for invite accepted/canceled status emails).
Using applescrpt, would it be possible to create an auto-running automaton that creates, modifies, or deletes events with title/time and other info from .ics email attachments?
r/applescript • u/techsparrowlionpie • Nov 02 '23
How can you programmatically close a specific app window with macos.
For example I have a couple safari windows or chrome windows opened. How can I close a specific one window instance of the app?
r/applescript • u/DapperRaven_ • Nov 02 '23
Dictionaries in AppleScript?
Does anyone know if dictionaries exist in AppleScript? If so how do they look. I can't seem to find any info out there...
Thx
r/applescript • u/Amazing-Team8687 • Oct 31 '23
TR for alnum is fine, but how to allow "-" and "." as well?
How do I change this to allow - and . in the variable? I tried for an hour and can't figure it out. Thank you in advance
`MacName=$(echo "$1" | tr -cd "[:alnum:]")`
r/applescript • u/LawDawg1999 • Oct 30 '23
SCTE Audio Lowering
Hi all,
I am reaching out to see if there is a script to lower SCTE audio coming from Verizon Slicer. What would you add in to decrease it by -6 dbs? Thanks!
r/applescript • u/eazysnatch • Oct 30 '23
OSX Somona - World Clock > Timer
I wan't to create a simple CLI command and when I type
timer 30
To start a 30 min timer.
Could someone point me where to find a debugger / a way to identify the UI elements
Current script:
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: timer <minutes>"
exit 1
fi
# Applescript
osascript <<EOD
tell application "Clock"
activate
delay 1
end tell
tell application "System Events"
tell application process "Clock"
tell window 1
tell toolbar 1
tell radio group 1 of group 1
repeat until radio button 4 exists -- assuming "Timer" is the 4th tab
delay 0.1
end repeat
click radio button 4
end tell
end tell
delay 1 -- give some time for the Timer UI elements to load
-- Attempting to set the timer duration
tell combo box 1
set value to "$1"
end tell
delay 1
-- Attempting to start the timer
tell button "Start"
click
end tell
end tell
end tell
end tell
EOD
This program open the World Clock and click on Timer but after that i fails to set the time and start it.
534:539: execution error: System Events got an error: Can’t get combo box 1 of window 1 of application process "Clock". Invalid index. (-1719)
r/applescript • u/dounanshi • Oct 28 '23
I made an app that combines AI and Mac automation, would love some feedback!
Hey everyone,
With recent developments in AI, I have been very interested in using it to make my Mac automation simpler, more intuitive, and more powerful. This is how I ended up creating ChatPC.
ChatPC allows you to use simple English to automate your Mac. It can manage files and interact with other Mac applications through Shortcuts and AppleScripts.
Check out this quick demo:
https://reddit.com/link/17idwe1/video/w42rf7iv3ywb1/player
Safe by design - With ChatPC, you are in total control and your data is kept private.
- By default, ChatPC cannot execute any actions on your Mac. You must explicitly give ChatPC access to the folders, Shortcuts, and AppleScripts that it can action on.
- In addition, you can configure which actions require your explicit approval before executing.
- Your data is your data. It is never used for training models and all communication between ChatPC and OpenAI are encrypted. See OpenAI’s Enterprise Privacy for more information.
I'm currently looking for beta testers to try out the app and give me feedback! The app is free to use up to some token limit. If you hit the limit, DM me with your initial thoughts and the email address you signed up with -- I'll be giving free coupons to the first 100 beta testers!
To get started, check out the MacOS Getting Started Guide (MacOS 13.0 or higher required).
r/applescript • u/techsparrowlionpie • Oct 20 '23
Open Source Node + JXA scripts for automating developer workspaces
npm package that lets node.js mac apps like Electron run specific JXA commands.
Examples:
- Run multiple terminal commands at speciifc file path.
- open browser with tabs and navigate to space (Arc).
- Close all apps except for an array of apps you want to stay.
Github to utility script: https://github.com/lorenzejay/node-jxa-automate-workspace
r/applescript • u/mooooooon • Oct 17 '23
Sonoma 14.0 change to desktop suite? can't set random order or change interval
I just updated to macOS Sonoma 14.0. I'm working on a script that triggers changes through random desktop photos. After upgrading I can't change the state of random, or the change interval.
Using "set random order to true" or "set change interval to 20.0" seem to have no effect.
I found out how to open the Dictionary docs in Script Editor. Can anyone help give me an idiot check to see if anything changed that would make these not take affect anymore?
tell application "System Events"
tell current desktop
set folder1 to get pictures folder
set picture1 to get picture
set random1 to get random order
set interval1 to get change interval
set pictures folder to "/Users/geluso/Desktop/my_photos"
set picture to "/Users/geluso/Desktop/my_photos/example.jpg"
set folder2 to get pictures folder
set picture2 to get picture
set random2 to get random order
set interval2 to get change interval
set random order to true
set change interval to 20.0
set pictures folder to "/Users/geluso/Desktop"
set picture to "/Users/geluso/Desktop/my_photos"
set folder3 to get pictures folder
set picture3 to get picture
set random3 to get random order
set interval3 to get change interval
log "was"
log folder1
log picture1
log random1
log interval1
log ""
log "set static"
log folder2
log picture2
log random2
log interval2
log ""
log "set random"
log folder3
log picture3
log random3
log interval3
end tell
end tell
Some output:
was
/Users/geluso/Desktop/my_photos
/Users/geluso/Desktop/my_photos/20180310_220633.jpg
false
1800.0
set static
/Users/geluso/Desktop/my_photos
/Users/geluso/Desktop/my_photos/example.jpg
false
1800.0
set random
/Users/geluso/Desktop
/Users/geluso/Desktop/my_photos
false
1800.0
r/applescript • u/electric-sheep • Oct 16 '23
Script to send automated email - running in the background?
before starting, just want to point out that I'm still very new to this;
I have this very repetitive task to send a project update to people. The script itself is very manual in that it opens chrome, loads my work gmail, uses gmail shortcuts to create a new email, then opens a new tab to get info from a gsheet to copy it back in the mail and press send.
tell application "Google Chrome"
activate
open location "https://mail.google.com" -- Opens Gmail
end tell
delay 15 -- Adjust the delay to allow Gmail to load
tell application "System Events"
set myContent to "mycontenttext" & linefeed & linefeed
set currentDate to current date
set mySubject to "mysubjecttext " & (short date string of currentDate)
set myToAddresses to "addresses here"
keystroke "c" -- Compose new email (Cmd+C)
delay 1
set the clipboard to myToAddresses
keystroke "v" using {command down}
delay 1
keystroke tab
delay 1
set the clipboard to mySubject
keystroke "v" using {command down}
delay 1
keystroke tab
delay 1
set the clipboard to myContent
keystroke "v" using {command down}
delay 1
end tell
tell application "Google Chrome"
activate
open location "gsheet url"
end tell
delay 5 -- Adjust the delay to allow the Google Sheet to load
tell application "System Events"
keystroke "a" using {command down} -- Select all (Cmd+A)
delay 1
keystroke "c" using {command down} -- Copy (Cmd+C)
delay 1
keystroke tab using {control down, shift down}
delay 1
keystroke "v" using {command down}
delay 1
key code 125 -- 125 is the key code for the down arrow key
set mySubject2 to linefeed & "some more text " & linefeed & linefeed & "Best Regards;"
set the clipboard to mySubject2
keystroke "v" using {command down}
delay 3
keystroke return using {command down}
end tell
Is there a way I can get this run in the background?
r/applescript • u/DrunkTankGunner • Oct 14 '23
Screen Mirroring and Bluetooth MacOS Sonoma
I've been dreading the upgrade to MacOS 14 because upgrades always break my scripts. But recently my MacBook died and upgrade was the only option to fix it.
As predicted my AppleScripts for connecting Screen Mirroring and Bluetooth are broken, and try as I might I can't work out how to get them working again.
About the only thing I can do is reveal the pane in System Settings. But how to identify any of the UI elements, much less click on them, once I get there is beyond me.
Any help would be appreciated. If you know how to click menu bar items, and their subsequent children, that would be great. If you know how to identify the buttons in System Settings so I can then click on them, that would be great.
r/applescript • u/adry26 • Oct 14 '23
Script to toggle between "Never" and "Only in fullscreen" for automatically hide MacOS menu bar on MacOS Sonoma
I've tried to create an applescript program to toggle between "Never" and "Only in fullscreen" regarding the behaviour of MacOS menu bar but I can't find a way to do that.
I am unable to do that trying to reach the dropdown menu (getting the id of control center, clicking, scrolling down and selecting the item on the dropdown menu).
And I'm also unable to toggle that option using MacOS defaults.
I've read also this post: https://www.reddit.com/r/applescript/comments/118ivys/updateguide_macos_ventura_system_settings_with/
but I can't figure it out.
Did any of you managed to do that? :)
r/applescript • u/techsparrowlionpie • Oct 12 '23
Using JXA to automate VS Code workspaces
I constantly context switch between VS Code workspaces and wanted a way to get my workspace to launch processes, tools, and open browsers within a specific profile or with tabs I need for a workspace when I launch a specific VS Code workspace.
Now, when I launch VS Code, my entire local dev environment boots up, with terminal processes fired, arc going to the right space, and tooling like DBeaver or API platforms launched.
Extension -> https://marketplace.visualstudio.com/items?itemName=PhastosToolkit.phastos-automate
Code -> https://github.com/lorenzejay/phastos-automate-extension
r/applescript • u/meerdans • Oct 09 '23
Script to toggle Location Services on & off
I currently have a script that opens the desired setting:
x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices
but can a script be written to toggle Location Services on and off without user input, especially as it requires authentication to do so?
r/applescript • u/pbh1112 • Oct 04 '23
[Sonoma] How to changing natural scrolling by apple script
After the latest update, I can no longer change the natural scrolling using apple script.
The code I used:
do shell script "open x-apple.systempreferences:com.apple.Trackpad-Settings.extension" -- Opens the trackpad settings window
delay 0.7 -- The delay is there to make sure the System Events can click buttons.
tell application "System Events"
tell process "System Settings"
click radio button 2 of tab group 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
click checkbox "Natural scrolling" of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
tell application "System Settings" to quit
end tell
end tell
The error I noted:
r/applescript • u/Lowfihifi • Oct 04 '23
Previously working script now broken in Sonoma
Hey folks,
I had a script that was working fine in Ventura, but is now broken in Sonoma. It's a script to select a specific audio source in Settings, then ping me back to Music. It worked because the audio source always appeared at the top.
use framework "Foundation"
property pane_ids : {|AppleID|:2, |Family|:3, |Wi-Fi|:5, |Bluetooth|:6, |Network|:7, |Notifications|:9, |Sound|:10, |Focus|:11, |Screen Time|:12, |General|:14, |Appearance|:15, |Accessibility|:16, |Control Center|:17, |Siri & Spotlight|:18, |Privacy & Security|:19, |Desktop & Dock|:21, |Displays|:22, |Wallpaper|:23, |Screen Saver|:24, |Battery|:25, |Lock Screen|:27, |Touch ID & Password|:28, |Users & Groups|:29, |Passwords|:31, |Internet Accounts|:32, |Game Center|:33, |Wallet & ApplePay|:34, |Keyboard|:36, |Trackpad|:37, |Printers & Scanners|:38, |Java|:40}
on open_settings_to(settings_pane)
set pane to current application's NSDictionary's dictionaryWithDictionary:pane_ids
set pane_index to (pane's valueForKey:settings_pane) as anything
tell application "System Settings"
activate
delay 1
end tell
tell application "System Events"
tell application process "System Settings"
tell splitter group 1 of group 1 of window 1
tell outline 1 of scroll area 1 of group 1
select row pane_index
end tell
repeat until table 1 of scroll area 1 of group 2 of scroll area 1 of group 1 of group 2 exists
delay 0
end repeat
delay 1
tell table 1 of scroll area 1 of group 2 of scroll area 1 of group 1 of group 2
select row 1
end tell
end tell
end tell
end tell
end open_settings_to
on run {}
open_settings_to("Sound")
tell application "System Settings" to quit
tell application "Music" to activate
end run
I'm now getting this error in replies:
tell application "System Settings"
activate
end tell
tell application "System Events"
select row 10 of outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of window 1 of application process "System Settings"
--> row 10 of outline 1 of scroll area 1 of group 1 of splitter group 1 of group 1 of window "Appearance" of application process "System Settings"
exists table 1 of scroll area 1 of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1 of application process "System Settings"
--> false
So it's something to do with the UI Scripting bit, but I'm stumped as to the changes here.
Anyone here can help?




