Thanks Jacques…
Unfortuantely I still have one problem!! If the hard drive contains special characters in the name, such as a space, this does not work. I added a character replacement subroutine, which I used in another terminal script I made-- which works perfect but for some alien reason, it’s not working correctly here! Can you explain why??
if theTarget =“Atari SH204:iJunk:Desktop-21-04-06-24:”
then after my charreplace section, theTarget becomes: “Atari\ SH204:iJunk:Desktop-21-04-06-24:”
Which makes absolutely no sense!! there should only be one backslash, and if I try to just put “\ " in the script, I get an error: “Expected “”” but found unknown token.” because applescript needs an escape character ""—which makes me scream because I don’t understand why two back slashes are showing up when I return theTarget.
I’d love an explanation for this!
tell application "Finder"
set foldername to "Desktop-" & (do shell script "date \"+%d-%m-%y\"")
set target_Link to ((path to home folder from user domain as string) & "iJunk:" as alias)
set foldernameoriginal to foldername
set r to 0
repeat
if not (exists folder foldername of target_Link) then
set theTarget to (make new folder at target_Link with properties {name:foldername}) as alias
exit repeat
end if
set r to r + 1
set foldername to foldernameoriginal & "-" & r
end repeat
set theTarget to my charreplace({"\\", "!", "@", "$", "^", "&", "*", "(", ")", ";", "'", "{", "}", "[", "]", "|", "<", ">", "?", "\"", " "}, {"\\\\", "\\!", "\\@", "\\$", "\\^", "\\&", "\\*", "\\(", "\\)", "\\;", "\\'", "\\{", "\\}", "\\[", "\\]", "\\|", "\\<", "\\>", "\\?", "\\\"", "\\ "}, theTarget as Unicode text)
set y to "mv ~/Desktop/* " & POSIX path of theTarget
do shell script (y)
end tell
to charreplace(original, replacement, textfield)
repeat with i from 1 to count original
set text item delimiters to item i of original
set textfield to listToText of me from textfield's text items between item i of replacement
set text item delimiters to ""
end repeat
return textfield
end charreplace
on listToText from l between d
set text item delimiters to d
tell l to beginning & ({""} & rest)
end listToText