Hello everyone,
I have spent many evenings, and weekends, trying to make the code below work, and I have hit a wall. Can someone please help?
The code has been based on a post found on Noodlesoft Forums - https://www.noodlesoft.com/forums/viewtopic.php?f=4&t=921#
The code seems to work fine when ‘Quit’ or ‘Clear Cache’ are selected. I need to ensure that apart from accepting the ‘Quit’ option the user input screen is always returned until the rules in Hazel are met - see below.
For example, I select continue and the cache is not as desired. In this instance I select Cancel and I am returned to the Continue, Quit or Clear Cache buttons. I select Clear Cache and the cache is cleared. I am returned back to the Continue, Quit or Clear Cache button options again, or even directly back to the user input dialog box with current name of the file ready to enter the new name details.
In another example, If I select Quit and then select Cancel, as Quit was selected in error, I should be returned to the Continue, Quit or Clear Cache buttons . The only Time the code should stop is if I have completed the new name, selected OK and the Hazel conditions are met, or if I select Quit and confirm by selecting OK.
My code currently opens the dialog box, as expected, upon selecting Continue. The dialog box shows either the Current Name or the Cached name plus Current Name. However, these details are not saved when selecting OK. The code seems to loop. This has happened in every version of the code used (including the base from the forum) not just the version of the code below.
The code does add the Green label when selecting OK under the rename dialog box, however, the file is NOT given the new name details entered. In this instance Hazel moves the file in error believing all is well, because it has a Green label.
One other glitch I have noted is a doubling of the cached name, which I would rather didn’t happen. I believe this is because the file is not saving as intended, does not have the relevant colour label and is being re-run by Hazel.
I am using Hazel to manually rename files after they have been from a remote location to a monitored folder and other rules have been processed. At present the code is run as an embedded script within Hazel, although it could run as a droplet if easier to work with or solve the problems I am experiencing.
Core objectives:
--tell application "Finder" to activate
tell application "Finder"
set currentName to name of theFile
set the clipboard to «class ktxt» of ((the clipboard as text) as record) # remove styles from the clipboard
set theCaption to the clipboard
copy length of theCaption to theCaptionlength # test the length of theCaption
if theCaptionlength > 100 then
set theCaption to text 1 thru 100 of theCaption # respect the maximum filename-length of 255. Some characters obviously don't count so the limit is set low here
else
set theCaption to theCaption # do nothing
end if
repeat
display dialog "Add New Details to the current file?" buttons {"Continue", "Quit", "Clear Cache"} default button 3
if the button returned of the result is "Continue" then
-- action for 1st button goes here
(display dialog "Add New Details:" default answer theCaption & currentName buttons {"Cancel", "OK"})
if button returned of (result) = "OK" then
set name of theFile to theCaption & currentName
# None = 0 | Orange = 1 | Red = 2 | Yellow = 3 | Blue = 4 | Purple = 5 | Green = 6 | Gray = 7
set label index of theFile to 6
display alert "All done! Image has been renamed for you."
return
else
return input
end if
return
else if the button returned of the result is "Quit" then
-- action for Quit 2nd Action button goes here
display dialog ("Quit current application?" & return & "(" & theFile & ")") buttons {"Cancel", "OK"} default button 2
if button returned of (result) = "ok" then
--set currentName to name of theFile
set label index of theFile to 4
tell application "System Events" to quit
return
end if
return
else
-- action for 3rd button goes here
tell application "System Events" to set the clipboard to ""
return input
return
end if
return
end repeat
end tell
My coding skills are very poor, but I am trying to learn as I go, so any changes you guys make please highlight, and explain what they do.This will help me learn where I have gone wrong, and hopefully get better over time.
Many thanks in advance for any help in making this work.