Resolving the ‘recreated paths’ of unarchived files

If you followed along from a previous post about automating password protection of zipped files on MacOS with a predefined password, you’ll notice that on unarchiving the zipped file with the previous Applescript or Automator workflow causes the path to the zip file to be recreated. So you’ll not only get your file but also a duplicate of the folder structure that the original file resided in. Not such a bad thing but a little clunky.

The solution is to modify the script so that Terminal automatically goes to the same folder containing the selected (source) file in the Finder and performs the zipping in that folder without any file paths added to either the archive filename or the source filename.

However, this presents a further problem because we need to navigate Terminal to the folder of the selected file but give it the text in quoted form, so as to be able to handle spaces in file names and folder names as well as give it paths in the POSIX path style (with slashes (/) and not colons(:)). A further niggle is that we have to remove the trailing colon from the filepath ( and slash from the POSIX path) of the sourcefile so that Terminal can successfully change to that directory to perform the zipping without needing to use filepaths in the zip command.

Note also that the variables runIt and changedir have to be declared as globals so that Terminal gets access to their contents as quoted form text strings for its own commands. Without global declaration these variables are considered local and so would only be accessible within the ‘Tell block’ in which they are created, which in this case is targeting the Finder, but are not available outside the block when Telling (targeting) another Application such as Terminal. This is a fundamental point with using AppleScript.

Remember also that if this script is going into an Automator Workflow then whenever you are making a call to a MacScript function you instead ‘tell myScript’

--applescript to automatically zip (encrypt) the selected file in the Finder with a stored password. (Version 2)
--To add this script as an Automater Service (so as to be right mouse button clickable ) remove the 'property parent' line below. 
-- Also remove --* from beginning of lines below.

-- * set script_path to (((path to scripting additions from local domain) as text) & " Library")
-- * set myScript to load script file script_path
-- * tell myScript to run

global changedir, runIt

property parent : load script alias (((path to scripting additions from local domain) as text) & " Library")

--get the currently selected file in the Finder or end if nothing selected
tell application "Finder"
	set sel to selection
	if sel is not {} then
		set myAlias to sel's item 1 as alias
		set myAliasPOSIX to POSIX path of myAlias
		display dialog "No file is selected for archiving"
	end if
end tell

--collect the filepath
set sourceFile to myAlias

--split the filepath into path and filename and the filename into its name and extension
--*tell myScript
set {theName, theFolder} to {name, folder} of DividePath(sourceFile) -- MacScript Function
set {theBase, theSuffix} to {base, suffix} of DivideFileName(theName) -- MacScript Function
--*end tell

--get quoted forms of posix versions for use with Terminal
set sourceFilePathPOSIX to POSIX path of sourceFile
set q_sourceFilePathPOSIX to quoted form of sourceFilePathPOSIX

--same with the target name of the zip file, tho don't need POSIX on text string but do need quoted form
set archiveFile to (theBase & ".zip")
set q_archiveFile to quoted form of archiveFile

--now remove the trailing colon (:) on the path to the sourcefile ( the directory we are telling Terminal to go to) 
set theFolderModCount to ((count of characters in theFolder) - 1)
--*tell myScript
set theFolderMod to TruncateString(theFolder, theFolderModCount) -- MacScript Function
--*end tell

-- now get the quoted form of the POSIX path
set theFolderPOSIX to POSIX path of theFolderMod
set q_theFolderPOSIX to quoted form of theFolderPOSIX
set q_theName to quoted form of theName

display dialog "Enter a password for encryption" default answer "password"
set password1 to (text returned of result)

set runIt to ("zip -P " & password1 & " " & q_archiveFile & " " & q_theName)
set changedir to ("cd " & q_theFolderPOSIX)

tell application "Terminal"
	ignoring application responses
		do script (changedir) in front window
		do script (runIt) in front window
	end ignoring
end tell

delay 3

tell application "Terminal"
end tell

Download the updated AppleScript file here

Archive ‘selected’ with

Download the updated Automator Workflow file here

Automator Archive selected with