Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Create Folder Action [to Zip?]

Create Folder Action [to Zip?]
Thread Tools
Junior Member
Join Date: Feb 2003
Location: Oz
Status: Offline
Reply With Quote
Nov 14, 2003, 04:30 AM
 
I realise that the Finder contextual menu has the "Create Archive of ..." to zip archive selected files

But is it possible to create a Folder Action script that will Zip archive a File dragged/dropped onto a folder

This would save time to bring up the contextual menu select the option, and then 'clean up' the files/archives on the Desktop

Any insights appreciated

tia

- be stirred but never shaken -
     
Addicted to MacNN
Join Date: Apr 2001
Location: europe
Status: Offline
Reply With Quote
Nov 14, 2003, 05:28 AM
 
I couldn't figure out how to make a .zip archive, but maybe this is a starting point:

Code:
on adding folder items to this_folder after receiving added_items repeat with i from 1 to the count of the added_items set this_file to item i of the added_items tell application "Finder" to set the_name to the name of this_file if (the_name does not end with ".cpgz") then tell application "BOMArchiveHelper" to open this_file end if end repeat end adding folder items to
Save as script in the Folder Action Scripts folder.
Nasrudin sat on a river bank when someone shouted to him from the opposite side: "Hey! how do I get across?" "You are across!" Nasrudin shouted back.
     
Pretzel  (op)
Junior Member
Join Date: Feb 2003
Location: Oz
Status: Offline
Reply With Quote
Nov 14, 2003, 07:25 AM
 
Originally posted by Developer:
[B]I couldn't figure out how to make a .zip archive, but maybe this is a starting point:

Thanks
Code:
on adding folder items to this_folder after receiving added_items repeat with i from 1 to the count of the added_items set this_file to item i of the added_items tell application "Finder" to set the_name to the name of this_file if (the_name does not end with ".cpgz") then tell application "BOMArchiveHelper" to open this_file end if end repeat end adding folder items to
apparently ditto from the cli is Zip aware and can archive along the lines of

Code:
/usr/bin/ditto -c -k -rsrc --keepParent /Users/user /some/place/user.zip
Can that be somehow incorporated within this scripting environment?

- be stirred but never shaken -
     
Sal
Dedicated MacNNer
Join Date: Sep 2000
Location: Cupertino, CA USA
Status: Offline
Reply With Quote
Nov 14, 2003, 09:32 AM
 
I'm not sure about the correct syntax of the shell command, but the structure of the script should be something like this.

NOTE:

1) create a "Done" folder in the attached folder and either move the file to process there or have the shell compress to that location. This will prevent the Folder Action from triggering again when the archive is created.

2) The "info for" command returns a record of an item's property. One of those properties is its "name extension". Use this property instead of checking the name to determine if a name extension exists or is the correct type.

Code:
on adding folder items to this_folder after receiving these_items tell application "Finder" if not (exists folder "Done" of this_folder) then make new folder at this_folder with properties {name:"Done"} end if set the destination_folder to folder "Done" of this_folder as alias set the destination_directory to POSIX path of the destination_folder end tell 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 info for this_item if this_item is not the destination_folder and the name extension of the item_info is not in {"zip", "sit"} then set the item_path to the quoted form of the POSIX path of this_item set the destination_path to the quoted form of (destination_directory & (name of the item_info) & ".zip") do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & item_path & " " & destination_path) end if end repeat end adding folder items to
For detailed info about Folder Actions, visit:

http://www.apple.com/applescript/folderactions/
(Last edited by Sal; Nov 15, 2003 at 12:34 AM. )
     
Pretzel  (op)
Junior Member
Join Date: Feb 2003
Location: Oz
Status: Offline
Reply With Quote
Nov 14, 2003, 09:13 PM
 
Thanks

[B]I'm not sure about the correct syntax of the shell command
Neither am I - but shall check it

In this regard does the syntax require that the command be inside quotation marks " " and the distinct paths inside "& ..&" within that?

do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & item_path & " " & destination_path)
- OR was that just your notation for illustration purposes

tia

- be stirred but never shaken -
     
Addicted to MacNN
Join Date: Apr 2001
Location: europe
Status: Offline
Reply With Quote
Nov 14, 2003, 09:44 PM
 
Strings are between " in Applescript and the & is the string concatenation symbol.

So the above notation concatenates the command (and some arguments and a space at the end) with the source path, with a single space (note that), with the destination path.
(Last edited by Developer; Nov 14, 2003 at 09:50 PM. )
Nasrudin sat on a river bank when someone shouted to him from the opposite side: "Hey! how do I get across?" "You are across!" Nasrudin shouted back.
     
Addicted to MacNN
Join Date: Apr 2001
Location: europe
Status: Offline
Reply With Quote
Nov 14, 2003, 09:49 PM
 
Originally posted by Sal:
I'm not sure about the correct syntax of the shell command.
What about making the BOMArchiveHelper scriptable?
Nasrudin sat on a river bank when someone shouted to him from the opposite side: "Hey! how do I get across?" "You are across!" Nasrudin shouted back.
     
Pretzel  (op)
Junior Member
Join Date: Feb 2003
Location: Oz
Status: Offline
Reply With Quote
Nov 15, 2003, 12:26 AM
 
Thanks to you both

Sal's script works perfectly - with Done as a sub-folder inside the outer Folder[with action script attached]

keeps the original in the outer folder and places the zipped archive in Done

Nice!

- be stirred but never shaken -
     
Sal
Dedicated MacNNer
Join Date: Sep 2000
Location: Cupertino, CA USA
Status: Offline
Reply With Quote
Nov 15, 2003, 12:37 AM
 
I made an important change to the script. I used the "quoted form" property when creating the POSIX paths. This property does encoding and places single quotes around the path string. This prevents names with non-allowed characters from causing an error in the script.

Enjoy! And thanks for the Shell example.

-- Sal
     
Junior Member
Join Date: Aug 2003
Location: Australia
Status: Offline
Reply With Quote
Nov 20, 2003, 06:49 AM
 
I click enable and configure folder actions, but it does nothing. Anybody know what the problem is?
Cheers,
Nick.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Nov 20, 2003, 06:54 AM
 
Originally posted by Sal:
I made an important change to the script. I used the "quoted form" property when creating the POSIX paths. This property does encoding and places single quotes around the path string. This prevents names with non-allowed characters from causing an error in the script.
-- Sal
You beat me to it - I wanted to show off my new AppleScript knowledge gleaned from this excellent stream : (p.s. you get to see Sal say "Snappy" too!)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:43 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2