When I run the following apple script test2 is applied even though the script was not…
- User
When I run the following apple script test2 is applied even though the script was not run a second time within 5 seconds-- This script assumes that SwitchResX daemon is installed and that Test1 and Test2 are valid display set names tell application "SwitchResX Daemon" -- Activate the application activate -- Get the current display set name apply display set "Test1" -- Check if it is different from Test1 if name is not equal to "Test1" then -- Apply Test1 display set apply display set "Test1" -- Display a notification display notification "Switched to Test1 display set" with title "SwitchResX" -- Set a variable to store the current time 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 script "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 a notification display notification "Switched to Test2 display set" with title "SwitchResX" end if else -- Display a notification display notification "Already using Test1 display set" with title "SwitchResX" end if end tell
- Assistant
The reason why "test2" is applied even though the script was not run a second time within 5 seconds is because the condition to check if the script is run again is incorrect.
In the script, the condition
if not (do shell script "[ -f ~/script_running ] && echo true || echo false") as boolean thenis checking for the existence of the "script_running" file to determine if the script is run again. However, the file is deleted at the end of the script, so it will always return false.To fix this issue, you can modify the condition to check if the file does not exist instead:
if not (do shell script "[ ! -f ~/script_running ] && echo true || echo false") as boolean thenThis condition checks if the "script_running" file does not exist, indicating that the script is not currently running. Therefore, if the file is not found, the script can proceed to apply "Test2" display set and display the corresponding notification.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.