Hi,
I have a folder action script which prompts the user to rename an added file and then open the newly named file. I use it all the time (numerous times per day) and have it attached to my screenshots folder (because I’m usually documenting bugs during GUI development and uploading them to bitbucket issue descriptions).
The script utilizes non-trivial renaming applescript code I found on the internet that works around the folder action triggering a new rename event when the folder thinks the newly renamed file is a new file which should trigger another folder action. It does this by first labeling the file with a color label (e.g. set the label index of this_item to the special_label_index
).
My suspicion is that the bug has to do with the setting of the label index. The file’s label gets successfully updated, as does the name, but the file object thereafter becomes unable to be used to open the file. E.g. this line of code no longer works:
open this_item
I was able to get around this issue by executing a shell script:
`do shell script (“open '” & (POSIX path of this_folder) & “/” & newName & “'”)
Note, I had to use POSIX path of this_folder
because POSIX path of this_item
would not work either.
I suspect the file’s label change as the culprit because in my test code, I eliminated the label change and (while the script asked me to change the file name twice - and the second time I didn’t change the already changed file name) it renamed the file successfully and also successfully opened the file using open this_item
.
I created a simplified version of the script (though I kept the more complex methods I obtained off the web. This version is the broken version which calls open this_item
. The file gets renamed, but does not open. Note, if you try this, you should use a file that you know the open command will succeed on, like an image or a text document:
property item_check_delay_time : 2
property folder_check_delay_time : 3
property special_label_index : 7
on adding folder items to this_folder after receiving added_items
set the added_items to my check_added_items(the added_items)
repeat with i from 1 to number of items in added_items
set this_item to item i of added_items as alias
tell application "Finder"
set theExtension to name extension of this_item
set this_file_name to text 1 through -((count theExtension) + 2) of (get name of this_item)
display dialog "Name Screenshot" default answer this_file_name buttons {"Cancel", "OK"} default button 2
set the replacement_string to the text returned of the result
set the name of this_item to (replacement_string & "." & theExtension as string)
open this_item
end tell
end repeat
return
end adding folder items to
on check_added_items(the added_items)
-- check the transfer status of every added file to determine
-- if each file has completed being moved into the attached folder
set the notbusy_items to {}
repeat with i from 1 to the number of items in the added_items
set this_item to (item i of the added_items)
if my check_busy_status(this_item) is false then
set the end of the notbusy_items to this_item
end if
end repeat
return the notbusy_items
end check_added_items
on check_busy_status(this_item)
-- a folder can contain items partially transfered
-- this routine will wait for all the folder contents to transfer
if the last character of (this_item as text) is ":" then
set the check_flag to false
repeat
-- look for any files within the folder that are still transferring
tell application "Finder"
try
set the busy_items to the name of every file of the entire contents of this_item ¬
whose file type begins with "bzy"
on error
set the busy_items to {}
end try
end tell
if the check_flag is true and the busy_items is {} then return false
-- pause for the indicated time
delay the folder_check_delay_time
-- set the flag and check again
set the check_flag to true
end repeat
else -- the passed item is a single file, suitcase, clipping, etc.
-- check the label of the item. If it is the marked label, then it's already been processed so ignore.
tell application "Finder"
if (the label index of this_item) as integer is the special_label_index then
return "ignore"
end if
end tell
set the check_flag to false
repeat
tell application "Finder"
set the item_file_type to the file type of this_item
end tell
if the check_flag is true and ¬
the item_file_type does not start with "bzy" then
tell application "Finder"
set the label index of this_item to the special_label_index
end tell
-- allow the Finder time to change the label
delay the item_check_delay_time
return false
else if the item_file_type does not start with "bzy" then
-- set the flag and check again
set the check_flag to true
end if
-- pause for the indicated time
delay the item_check_delay_time
end repeat
end if
end check_busy_status
I am guessing that Sierra changed something WRT labels in the finder that broke certain functions of the file object in applescript. Or maybe it works, but just differently.
Oh yeah, one more thing, I wrapped the code in a try block and displayed the error string. I didn’t save it, but I’m certain it said “Parameter error” and that there was not as associated error code in the error string.