I do not know Applescript, but somehow I have created a script which creates two different folders i.e. ART and PDF at my current selected window and renames two existing folders.
There is one InDesign file with .indd extension and one text file “Instructions.txt” located at the root of the currently selected window.
What I want to achieve is to move the file with .indd extension to the newly created ART folder and
delete the “Instructions.txt” file.
Here is my script:
tell application “Finder”
set theLocation to (target of front window) as string
make new folder at theLocation with properties {name:“ART”}
make new folder at theLocation with properties {name:“PDF”}
set name of folder “Document Fonts” of folder theLocation to “FONTS”
set name of folder “Links” of folder theLocation to “IMAGES”
end tell
tell application "Finder"
try
set theLocation to (target of front window) as string
tell folder theLocation
if not (exists folder "ART" of it) then
make new folder at it with properties {name:"ART"}
end if
if not (exists folder "PDF" of it) then
make new folder at it with properties {name:"PDF"}
end if
if (exists folder "Document Fonts" of it) then
set name of folder "Document Fonts" of it to "FONTS"
end if
if (exists folder "Links" of it) then
set name of folder "Links" of it to "IMAGES"
end if
set titems to every file of it whose name extension is "indd"
if titems ≠{} then
move item 1 of titems to folder "ART"
end if
end tell
if (exists file "Instructions.txt" of folder theLocation) then
move (file "Instructions.txt" of it) to trash
end if
on error e number n
display alert e & " " & n
end try
end tell
Thanks McUsrII for your script, I will check it thoroughly. However, I have done it at my end and it is working perfectly fine. Here is my script
tell application “Finder”
set theLocation to (target of front window) as string
set Artf to make new folder at theLocation with properties {name:“ART”}
make new folder at theLocation with properties {name:“PDF”}
set name of folder “Document Fonts” of folder theLocation to “FONTS”
set name of folder “Links” of folder theLocation to “IMAGES”
tell application "Finder"
set allFiles to every file of entire contents of (theLocation as alias) whose name extension is "txt"
repeat with i from 1 to number of items in allFiles
set currentFile to item i of allFiles
tell application "Finder"
delete currentFile
end tell
end repeat
end tell
tell application "Finder"
set allFiles to every file of entire contents of (theLocation as alias) whose name extension is "indd"
repeat with i from 1 to number of items in allFiles
set currentFile to item i of allFiles
tell application "Finder"
move currentFile to (Artf as alias)
end tell
end repeat
end tell
Well. Mine seems a little paranoid, but on the other hand, it won’t break that easily if any assumptions fails, like under testing.
Your script looks good, but you should really keep it all inside one Finder block, or at least not nest them, I’m not sure if nesting blocks for the same app should be avoided for other than aestaethic purposes though.
Too many its, they are only required for the location while creating a new folder
tell application "Finder"
set theLocation to (target of front window)
tell theLocation
if not (exists folder "ART") then make new folder at it with properties {name:"ART"}
if not (exists folder "PDF") then make new folder at it with properties {name:"PDF"}
if (exists folder "Document Fonts") then set name of folder "Document Fonts" to "FONTS"
if (exists folder "Links") then set name of folder "Links" to "IMAGES"
try
move (1st file whose name extension is "indd") to folder "ART"
delete file "Instructions.txt"
on error e
display alert e
end try
end tell
end te
Hi, the script is working fine except one little error notification:
“Finder got an error: Can’t get file “Instructions.txt”. -1728”
I like the way you scripted using if conditions. Mine gives error if a folder already exists, but yours script is smart enough to check that.
As I said that i do not know applescript. I just try to manipulate the things collected from here and there. What you said about “block” is apart my knowledge and hence needs your time to learn what you are trying to say. Could you edit my script, keeping it in a single block, it will be an additional knowledge for me.
I also think you’ll learn the most by just read/copy and paste the items from my script, where it doesn’t fail, when some assumption aren’t correct. like that the instruction.txt isn’t there, then my file exists test, will just skip it, whereas you may wish to get a notification, or a chance to abort the script if it has already run.
the error occurs if the file Instruction does not exist. You can check the existence too.
In this version the script does not display any error message
tell application "Finder"
set theLocation to (target of front window)
tell theLocation
if not (exists folder "ART") then make new folder at it with properties {name:"ART"}
if not (exists folder "PDF") then make new folder at it with properties {name:"PDF"}
if (exists folder "Document Fonts") then set name of folder "Document Fonts" to "FONTS"
if (exists folder "Links") then set name of folder "Links" to "IMAGES"
if (exists file "Instructions.txt") then delete file "Instructions.txt"
try
move (1st file whose name extension is "indd") to folder "ART"
end try
end tell
end tell
Wow!! This is amazing. The script is short but it is giving an error if run twice on the same folder.
The error 1: (if it didn’t find the .indd file)
“Finder got an error: Can’t get file 1 of folder “path to folder” of startup disk whose name extension of it = “indd”.”
and
Error 2: (if it didn’t find the “Instructions.txt” file)
“Finder got an error: Can’t get file “Instructions.txt” of folder “path to folder” of startup disk.”
Therefore, I tried to edit it by copying the elements from script, here is the edited one:
tell application “Finder”
set theLocation to (target of front window)
tell theLocation
if not (exists folder “ART”) then make new folder at it with properties {name:“ART”}
if not (exists folder “PDF”) then make new folder at it with properties {name:“PDF”}
if (exists folder “Document Fonts”) then set name of folder “Document Fonts” to “FONTS”
if (exists folder “Links”) then set name of folder “Links” to “IMAGES”
set titems to every file of it whose name extension is "indd"
if titems ≠{} then
move item 1 of titems to folder "ART"
end if
tell application "Finder"
set allFiles to every file of entire contents of (theLocation as alias) whose name extension is "txt"
repeat with i from 1 to number of items in allFiles
set currentFile to item i of allFiles
tell application "Finder"
delete currentFile
end tell
end repeat
end tell
end tell
end tell
It is now too long, but working fine, if runs multiple times on the same folder. Could you cross check and shorten the move and delete codes.
Thanks, it is working perfectly fine. I just add a little code “with replacing”
tell application “Finder”
set theLocation to (target of front window)
tell theLocation
if not (exists folder “ART”) then make new folder at it with properties {name:“ART”}
if not (exists folder “PDF”) then make new folder at it with properties {name:“PDF”}
if (exists folder “Document Fonts”) then set name of folder “Document Fonts” to “FONTS”
if (exists folder “Links”) then set name of folder “Links” to “IMAGES”
if (exists file “Instructions.txt”) then delete file “Instructions.txt”
try
move (1st file whose name extension is “indd”) to folder “ART” with replacing
end try
end tell
end tell