A Folder Action to encode a script to be Opened in Editor
To use:
-
Place this script in
~/Library/Scripts/Folder Action Scripts/
-
Use Folder Actions Setup to associate this script with a particular folder
(Do: Control-click a folder in Finder » Services » Folder Actions Setup)
Click to open the below script in Script Editor
(Also published as a GitHub Gist which I keep up to date as necessary)
property done_foldername : "URL Encoded Scripts"
on adding folder items to this_folder after receiving these_items
-- If the result folder doesn't exist, then create it
tell application "Finder"
if not (exists folder done_foldername of this_folder) then
make new folder at this_folder with properties {name:done_foldername}
set current view of container window of this_folder to list view
end if
set the target_folder to folder done_foldername of this_folder
log "info for this_folder: " & (this_folder)
end tell
-- Process each file newly-added to this_folder
try
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to the info for this_item
if (alias of the item_info is false and the type identifier of the item_info is "com.apple.applescript.text") or (the name extension of the item_info is "applescript") then
tell application "Finder"
-- LOOK FOR EXISTING MATCHING ITEMS IN THE DESTINATION FOLDER
-- IF THERE ARE MATCHES, THEN RENAME THE CONFLICTING FILES INCREMENTALLY
my resolve_conflicts(this_item, target_folder)
-- MOVE THE ITEM TO THE DESTINATION FOLDER
set the target_file to (move this_item to the target_folder with replacing) as alias
set target_folderName to the POSIX path of (target_folder as string) as string
set the scriptInputName to target_folderName & the text of (name of (info for target_file))
set the scriptInputExtension to "." & (the text of (name extension of (info for target_file)))
set the scriptTrimName to my trimText(scriptInputName, scriptInputExtension, "end")
set the scriptOutputName to scriptTrimName & ".urlencoded"
set the scriptInputName to the quoted form of scriptInputName
set the scriptOutputName to the quoted form of scriptOutputName
end tell
-- PROCESS THE ITEM
process_item(scriptInputName, scriptOutputName)
else
tell application "Finder"
set myErrorMessage to "Expecting " & scriptTrimName & " to have been exported as Text "
activate
display dialog myErrorMessage buttons {"Cancel"} default button 1 giving up after 120
end tell
end if
end repeat
on error error_message number error_number
if the error_number is not -128 then
tell application "Finder"
activate
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end if
end try
end adding folder items to
on resolve_conflicts(this_item, target_folder)
tell application "Finder"
set the quotedName to quoted form of POSIX path of this_item
log "quotedName: " & quotedName
set the file_name to the name of this_item
if (exists document file file_name of target_folder) then
set file_extension to the name extension of this_item
if the file_extension is "" then
set the trimmed_name to the file_name
else
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
end if
set the name_increment to 1
repeat
set the new_name to (the trimmed_name & "." & (name_increment as string) & "." & file_extension) as string
if not (exists document file new_name of the target_folder) then
-- rename to conflicting file
set the name of document file file_name of the target_folder to the new_name
exit repeat
else
set the name_increment to the name_increment + 1
end if
end repeat
end if
end tell
end resolve_conflicts
-- this sub-routine processes files
on process_item(scriptInputName, scriptOutputName)
-- NOTE that the variable this_item is a file reference in alias format
-- FILE PROCESSING STATEMENTS GOES HERE
try
with timeout of 3 seconds
set shellCommand to "echo '<a href = \"applescript://com.apple.scripteditor?action=new&script=' > " & scriptOutputName & " && cat " & scriptInputName & " | python3 -c \"import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))\" >> " & scriptOutputName & " && echo '\">Open in Script Editor</a>' >> " & scriptOutputName
do shell script shellCommand
end timeout
on error error_message
tell application "Finder"
activate
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end try
end process_item
on trimText(theText, theCharactersToTrim, theTrimDirection)
set theTrimLength to length of theCharactersToTrim
if theTrimDirection is in {"beginning", "both"} then
repeat while theText begins with theCharactersToTrim
try
set theText to characters (theTrimLength + 1) thru -1 of theText as string
on error
-- text contains nothing but trim characters
return ""
end try
end repeat
end if
if theTrimDirection is in {"end", "both"} then
repeat while theText ends with theCharactersToTrim
try
set theText to characters 1 thru -(theTrimLength + 1) of theText as string
on error
-- text contains nothing but trim characters
return ""
end try
end repeat
end if
return theText
end trimText