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
1
Upvotes
View all comments
Show parent comment
1
u/Tdmoreno21 Nov 29 '23
I apologize for the mess and I will try to clean it up. I am a school teacher and tried to read the pinned post on how to post code but couldn't get through all of the steps bc I had to get to planning for my students. I apologize in advance if my explanation is unclear as I have little experience with any of this. I had to use the variable 'name' as that was what was listed as what appears to be the only relevant item when I open the dictionary entry under apple script editor for the app switchresx. I can execute a change to the display set so the script responds to the variable name but it does not stop after the first change and simply waits 5 seconds and then goes on to change the display set to the second set. I was hoping to get it to stop unless I run it again within 5 seconds.