Hi, this is my first post here. I’ve strated working on Macs a month ago in my new job and now i’m trying to learn Applescript to improve some things here at the office.
We have an imagebank that first needs to be classified by an operator. Then when have to grab the high definition files, make a preview file (400400px) and a thumbnail (120120px) and put them in special folders.
I’ve managed to create a script that does all of that, but my boss also wants the directories to be diferent. Let me explain…
We have the high-ress files in a folder with a lot of subfolders an so on on a disk, and we want a script that moves the high-ress files from where they are, to an identical tree but in another disk while creating the preview and thumbnail files.
as i said, the only thing i can’t do is to make the script move the high ress file, and to create the preview and thumbnail directly in the other disk (always with the same folder tree).
here is the code…
property pwg_foldername : "pwg_high"
property tn_foldername : "thumbnail"
--thumbnails y pgws are created in .jpg
property low_ext : "jpg"
on adding folder items to this_folder after receiving these_items
tell application "Finder"
if not (exists folder pwg_foldername of this_folder) then
make new folder at this_folder with properties {name:pwg_foldername}
end if
set pwg_folder to (folder pwg_foldername of this_folder) as alias
if not (exists folder tn_foldername of this_folder) then
make new folder at this_folder with properties {name:tn_foldername}
end if
set tn_folder to (folder tn_foldername of this_folder) as alias
end tell
try
repeat with i from 1 to number of items in these_items
tell application "Finder"
set this_item to item i of these_items
my change_ext(this_item, "")
set the file_name to my change_ext(this_item, low_ext)
set the source_file to (this_item) as alias
end tell
process_pwg(source_file, file_name, pwg_folder, tn_folder)
end repeat
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 adding folder items to
--this changes the extension of the path so the preview and thumbail are saved as jpg
on change_ext(this_item, new_extension)
tell application "Finder"
set the file_name to the name of this_item
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
if the new_extension is "" then
set target_name to file_name
set target_extension to file_extension
else
set target_extension to new_extension
set target_name to (the trimmed_name & "." & target_extension) as string
end if
end tell
return the target_name
end change_ext
--this scales the highress file to create the preview and the thumbnail
on process_pwg(source_file, file_name, pwg_folder, tn_folder)
try
set the pwg_path to ((pwg_folder as string) & file_name) as string
set the tn_path to ((tn_folder as string) & file_name) as string
with timeout of 900 seconds
tell application "Image Events"
launch
set this_image to open file (source_file as string)
scale this_image to size 400
save this_image as JPEG in file pwg_path with icon
close this_image
set this_image to open file (pwg_path as string)
scale this_image to size 120
save this_image as JPEG in file tn_path with icon
close this_image
end tell
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_pwg
To be more acurate, i want to grab the path of “this_folder” (wich could be HD:imagebank:gallery:people:kids) and change it to HD2:webgallery:images:people:kids). But i can’t set those values fixed, because i want it to be a dynamic folder actin that works on every folder, no matter where it is, nor if we change it after. The only things i know will NEVER change are these “HD:imagebank:gallery:” and "HD2:webgallery:images: .
Thanks for your help,
and sorry for my crude english ( i hope it wasn’t too hard to understand ).
Thanks again.
Martin.
Model: G4
Browser: Firefox 1.5.0.5
Operating System: Mac OS X (10.4)
Hi Martin - one idea to do this that’s relatively easy in my opinion would be to take the existing path of the file you want to copy, change it to a posix path "ie: (HD/users/desktop/…) and then just change the root…the HD in this case. Then you can can copy the image to the new location - you would create this new location using the shell command “mkdir -p /path” (the "-p"flag of mkdir (make directory) tells it to create the intermediate directories as needed.)
this is someone difficult to explain - but see the code below - and you might see what I’m talking about: (you’ll need to change your variables to suit. Also - in the sed statement - change the the volume names to what you need…in your case I guess that would be sed ‘s|HD|HD2|’"
set sourceFilepath to alias "BACKUP_1TB:Folder1:Untitled.scpt"
set sourceFilePathPOSIX to POSIX path of sourceFilepath
set destFilePathbase to do shell script "dirname " & sourceFilePathPOSIX & "| sed 's|BACKUP_1TB|Macintosh\\\\ HD|'"
do shell script "mkdir -p " & destFilePathbase
do shell script "cp " & sourceFilePathPOSIX & space & destFilePathbase
thanks for your reply!!!
like a fool i didn’t suscribe to my own post… 
i came up with a far more raw solution, but it works…
let me know what your think of it
property pwg_foldername : "pwg_high"
property tn_foldername : "thumbnail"
--thumbnails y pgws se crean en .jpg
property low_ext : "jpg"
-- ORIGEN: " Marto:imagenesBETA:imagenes: " 28 chars
-- DESTINO: " Marto:imagenesALFA:galerias: " 28 chars
on adding folder items to this_folder after receiving these_items
try
tell application "Finder"
set this_item to item 1 of these_items
set relative_path to text 29 thru end of (container of this_item as string)
set that_folder to "Marto:imagenesALFA:galerias:" & relative_path
set test_pwg to (that_folder & pwg_foldername)
set test_tn to (that_folder & tn_foldername)
if not (exists folder test_pwg) then
make new folder at that_folder with properties {name:pwg_foldername}
end if
set pwg_folder to test_pwg as alias
if not (exists folder test_tn) then
make new folder at that_folder with properties {name:tn_foldername}
end if
set tn_folder to test_tn as alias
end tell
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
try
repeat with i from 1 to number of items in these_items
tell application "Finder"
set this_item to item i of these_items
my change_ext(this_item, "")
set the file_name to my change_ext(this_item, low_ext)
set the source_file to (this_item) as alias
end tell
process_pwg(source_file, file_name, pwg_folder, tn_folder)
tell application "Finder" to move this_item to the that_folder
end repeat
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 adding folder items to
on change_ext(this_item, new_extension)
tell application "Finder"
set the file_name to the name of this_item
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
if the new_extension is "" then
set target_name to file_name
set target_extension to file_extension
else
set target_extension to new_extension
set target_name to (the trimmed_name & "." & target_extension) as string
end if
end tell
return the target_name
end change_ext
on process_pwg(source_file, file_name, pwg_folder, tn_folder)
try
set the pwg_path to ((pwg_folder as string) & file_name) as string
set the tn_path to ((tn_folder as string) & "tn_" & file_name) as string
with timeout of 900 seconds
tell application "Image Events"
launch
set this_image to open file (source_file as string)
scale this_image to size 400
save this_image as JPEG in file pwg_path with icon
close this_image
set this_image to open file (pwg_path as string)
scale this_image to size 120
save this_image as JPEG in file tn_path with icon
close this_image
end tell
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_pwg
thanks.
Your scripts looks good - if it works for you - then that’s all that matters right? The only thing you could potentially run into trouble with is the way you are referencing the path ie: the 29 characters. that will break if for some reason you ever change the name of your hd or any of the other folders to something that totals a character count of something other than 29. Aside from that - it looks like your well on your way to Applescripting.