I have a folder with a bunch of subfolders, each containing multiple pictures.
I would like to compress the contents of each subfolders into a ZIP file that’s named after the subfolder. The ZIP is then moved to another folder.
I’m trying to use Automator and workflows, but it’s just not working for me. Should this be more of an AppleScript? Any pointers? Pretty new to AppleScript though.
--set sourceFolder to choose folder "Select the source folder"
--set destFolder to choose folder "Select the destination folder"
set sourceFolder to "the:path:to:the:source:folder:" as alias
set destFolder to "the:path:to:the:destination:folder:"
tell application "System Events"
set theSubFolders to path of folders of sourceFolder
end tell
set destFolder to destFolder as text # In case destFolder was an alias or a file reference
if destFolder does not end with ":" then set destFolder to destFolder & ":"
repeat with aSubFolder in theSubFolders
my zipIt(aSubFolder as text, destFolder)
end repeat
--=====
on zipIt(original, destFolder) (* original and destFolder are strings *)
local Nom, ext, dossier, nomZip, source, dest
tell application "System Events" to tell disk item original
set Nom to name
set nbItems to count (get disk items whose visible is true)
end tell
if nbItems > 0 then
set nomZip to Nom & ".zip"
set source to quoted form of POSIX path of original
set dest to destFolder & nomZip
tell application "System Events"
if exists disk item dest then
tell (current date) to set stamp to "_" & (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text) & ".zip"
set dest to text 1 thru -5 of dest & stamp
end if
end tell
set dest to quoted form of POSIX path of dest
do shell script "ditto -ck " & source & " " & dest
end if
end zipIt
--=====
Yvan KOENIG (VALLAURIS, France) lundi 29 juin 2015 11:19:54
I clicked on [Open this Scriplet in your Editor] and got exactly what I posted and it worked flawlessly.
Here you may read the events log :
tell application "Script Editor"
choose folder
choose folder
end tell
tell application "System Events"
get path of every folder of alias "SSD 500:Users:<my account>:Desktop:batch_AWx2iWork:"
get name of disk item "SSD 500:Users:<my account>:Desktop:batch_AWx2iWork:batch_AWx2iWork_intel:"
get every disk item of disk item "SSD 500:Users:<my account>:Desktop:batch_AWx2iWork:batch_AWx2iWork_intel:" whose visible = true
exists disk item "SSD 500:Users:<my account>:Desktop:Storage:batch_AWx2iWork_intel.zip"
end tell
tell current application
do shell script "ditto -ck '/Users/<my account>/Desktop/batch_AWx2iWork/batch_AWx2iWork_intel/' '/Users/<my account>/Desktop/Storage/batch_AWx2iWork_intel.zip'"
end tell
tell application "System Events"
get name of disk item "SSD 500:Users:<my account>:Desktop:batch_AWx2iWork:batch_AWx2iWork_PPC:"
get every disk item of disk item "SSD 500:Users:<my account>:Desktop:batch_AWx2iWork:batch_AWx2iWork_PPC:" whose visible = true
exists disk item "SSD 500:Users:<my account>:Desktop:Storage:batch_AWx2iWork_PPC.zip"
end tell
tell current application
do shell script "ditto -ck '/Users/<my account>/Desktop/batch_AWx2iWork/batch_AWx2iWork_PPC/' '/Users/<my account>/Desktop/Storage/batch_AWx2iWork_PPC.zip'"
end tell
I triple checked :
– the script doesn’t fail when the source folder doesn’t contain folders
– it doesn’t fail when one or several subfolders are empty.
– I used Copy - Paste to extract the script from the message. It’s bad practice because sometimes the parser used by this forum introduce odd characters. In this case, the resulting script behaved flawlessly.
If I understand well, what you describe is a compile error.
It means that you weren’t trying to compile my exact code.
I was able to reproduce it after deleting or commenting out the instruction “end repeat”.
It’s really boring to read comments about scripts which were modified by the asker before testing them.
Yvan KOENIG (VALLAURIS, France) mardi 30 juin 2015 10:00:21
I am opening it in editor exactly as you instructed. I fill in the source and destination folders, click Run (not Compile) and a window pops up that wants me to choose a folder. I’ll pick the destination folder and then a window pops up again asking me to choose a folder again. I’ll pick the destination folder again and then nothing happens.
The two folders I’m working with are
Pictures
Zipped
Both are on my desktop.
I’m not altering anything in your script…I’m a noob and wouldn’t mess with anything, but perhaps I’m not setting the folder locations correctly?
Running the script grabbed from the thread automatically compile the script.
It’s during this step that you got the message reported in your preceding message.
Getting it means that the script was not matching what I posted.
Now, you write that you are asking to select the source folder and the destination one.
It’s what the script is supposed to do.
Then you wrote that the script did nothing.
As far as I know, there are two cases where the script behaves this way :
(1) the source folder doesn’t contain folders
(2) the folders embedded in the source one are empty.
Attached is a version which will explicitly warn you when such situation arise.
-- set sourceFolder to choose folder "Select the source folder"
-- set destFolder to choose folder "Select the destination folder"
set sourceFolder to "the:path:to:the:source:folder:" as alias
set destFolder to "the:path:to:the:destination:folder:"
tell application "System Events"
set theSubFolders to path of folders of sourceFolder
end tell
if (count theSubFolders) > 0 then
set destFolder to destFolder as text # In case destFolder was an alias or a file reference
if destFolder does not end with ":" then set destFolder to destFolder & ":"
repeat with aSubFolder in theSubFolders
my zipIt(aSubFolder as text, destFolder)
end repeat
else
tell application "SystemUIServer" to display dialog "the folder "" & sourceFolder & "" is empty !" buttons {"Continue"} default button 1
end if
--=====
on zipIt(original, destFolder) (* original and destFolder are strings *)
local Nom, ext, dossier, nomZip, source, dest
tell application "System Events" to tell disk item original
set Nom to name
set nbItems to count (get disk items whose visible is true)
end tell
if nbItems > 0 then
set nomZip to Nom & ".zip"
set source to quoted form of POSIX path of original
set dest to destFolder & nomZip
tell application "System Events"
if exists disk item dest then
tell (current date) to set stamp to "_" & (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text) & ".zip"
set dest to text 1 thru -5 of dest & stamp
end if
end tell
set dest to quoted form of POSIX path of dest
do shell script "ditto -ck " & source & " " & dest
else
tell application "SystemUIServer" to display dialog "the folder "" & original & "" is empty !" buttons {"Continue"} default button 1
end if
end zipIt
--=====
Yvan KOENIG (VALLAURIS, France) mardi 30 juin 2015 15:36:28
I was changing the first two lines, thinking that I needed to put the folder locations in the script.
Would that be possible? To basically code into the script to always look at the Pictures folder and move the ZIPs to the Zipped folder so I wouldn’t have to input them every time?
(1) so, you were really applying the script to empty folders.
(2) I edited the script in my message above.
You just have to edit two instructions :
set sourceFolder to "the:path:to:the:source:folder:" as alias
set destFolder to "the:path:to:the:destination:folder:"
Of course you will have to replace [b]the:path:to:the:source:folder:[/b] and the:path:to:the:destination:folder: by the real pathnames.
Don’t forget the ending colons and the “as alias” part in the first instruction.
The script was tested with the choose folder instructions so the variables contained aliases ending with the colon.
Yvan KOENIG (VALLAURIS, France) mercredi 1 juillet 2015 10:41:50