Getting AppleScript to work

End user supplied how to documents and discussion.

Getting AppleScript to work

Postby bates » Fri Oct 18, 2013 4:59 pm

Hi guys,

Someone posted a handy AppleScript to take a file or folder and created an encrypted zip archive.

Here it is:

Code: Select all
on run {input, parameters}
                set dialogResults to display dialog "Name for zipped file (no extension)" default answer "Archive" buttons {"OK", "Cancel"} default button "OK"
           if button returned of dialogResults is "OK" then
      set passwd to text returned of (display dialog "password for zipped file" default answer "password" buttons {"OK", "Cancel"} default button "OK")
      
      set archiveName to text returned of dialogResults
      
      tell application "Finder"
         set archiveFileName to archiveName & ".zip"
         -- Append on a number if file exists.
         set suffix to 1
         set theFileExists to true
         repeat while theFileExists
            try
               set archiveFile to ((container of (item 1 of input) as Unicode text) & archiveFileName)
               if exists file archiveFile then
                  set archiveFileName to archiveName & suffix & ".zip"
                  set suffix to suffix + 1
               else
                  set theFileExists to false
               end if
            end try
         end repeat
      end tell
      set itemStr to ""
      repeat with thisItem in input
         set itemPath to quoted form of (POSIX path of thisItem)
         tell application "Finder"
            set parentFolder to POSIX path of (container of thisItem as alias)
         end tell
         set itemStr to itemStr & " " & itemPath
      end repeat
      set zipFile to quoted form of (parentFolder & archiveFileName)
      set cmd to "zip -P " & passwd & " -rj " & zipFile & " " & itemStr & " -x *.DS_Store"
      do shell script cmd
   end if
   return
end run

The instructions showed how to build this in Automator to create a service in the Finder, and that worked well.

I thought it might be cool to create a QuicKey to do this, too. So I copied the AppleScript into an AppleScript QuicKey, but it doesn't work. My guess is that it doesn't know what I've selected in the Finder.

When you create the Automator Service, there's an option "Service receives files and folders in Finder". This must be how the Finder selection is passed to Automator.

Any ideas on how to do this in QuicKeys?

Many thanks.
bates
 
Posts: 10
Joined: Thu Aug 23, 2007 6:30 pm

Re: Getting AppleScript to work

Postby gb2 » Fri Oct 18, 2013 5:31 pm

just use a Menu Selection step to pick the item from the Services menu.
User avatar
gb2
Site Admin
 
Posts: 2878
Joined: Mon Feb 06, 2006 6:22 pm

Re: Getting AppleScript to work

Postby bates » Fri Oct 18, 2013 5:47 pm

Thanks, that's a good idea, and I'm sure it will work.

Since I'd like to know a little more about QuicKeys and AppleScript, is there a way to get it to work directly. Say, for example, that I didn't install the Service in the first place.

Is there a way to make a change to the AppleScript that I entered?

D
bates
 
Posts: 10
Joined: Thu Aug 23, 2007 6:30 pm

Re: Getting AppleScript to work

Postby gb2 » Mon Oct 21, 2013 10:37 am

the structure of the AppleScript Automator is using is different then what QuicKeys uses. You would probably have to remove the "on run" handler statements and replace the input variable that it would be getting from the Finder as to which file/folders were selected with your own AppleScript code to get that info and any other changes it might need to get it to work. You might try getting help at MacScripter for modifying it to work in QuicKeys.
User avatar
gb2
Site Admin
 
Posts: 2878
Joined: Mon Feb 06, 2006 6:22 pm


Return to QKM How Tos

Who is online

Users browsing this forum: No registered users and 5 guests

cron