MacOS open decrypted file in TextEdit

Discussion related to AES Crypt, the file encryption software for Windows, Linux, Mac, and Java.
Post Reply
dosborn
Posts: 1
Joined: Fri Jan 24, 2014 12:10 am
Location: Longmont, CO

MacOS open decrypted file in TextEdit

Post by dosborn »

Since I mostly use aescrypt for text files, I did a quick hack to make an option to open them in TextEdit and only write the decrypted file temporarily to disk.
There are still a lot of enhancements that could be made but it meets my current needs and hope it's useful for someone else.
You can either create your own app or use the command below, opening the AESCrypt app and replacing the contents.
sudo /Applications/Utilities/AppleScript\ Editor.app/Contents/MacOS/AppleScript\ Editor

Code: Select all

-- Written by Doug Reed Copyright 2009
-- Released as Freeware.
-- 2014-01-20 - Modified by David Osborn <oss-dev@daoConsulting.com> to allow for opening into TextEdit and password confirmation on encryption

on open argv
	try
		set tFile to argv -- or use an alias to any file
		set tPath to quoted form of (POSIX path of tFile) -- the shell's form. quoted form of is required if the path might include spaces.
		set tName to name of (info for tFile) -- standard AppleScript
		set my_extension to ""
		set _length to (count of tName)
		if _length > 4 then
			set my_extension to text ((the number of characters of tName) - 3) thru -1 of tName
		end if
		try
			
			if my_extension = ".aes" or my_extension = ".AES" then
				set my_direction to "decryption"
				display dialog "Enter password for " & ¬
					my_direction ¬
						with title ¬
					"AESCrypt" with icon 1 ¬
					default answer ¬
					"" buttons {"Cancel", "Continue", "Decrypt to TextEdit"} ¬
					default button ¬
					"Continue" cancel button ¬
					"Cancel" with hidden answer
				set {my_pass, outputMode} to {quoted form of text returned, button returned} of result
			else
				set my_direction to "encryption"
				
				display dialog "Enter password for " & ¬
					my_direction ¬
						with title ¬
					"AESCrypt" with icon 1 ¬
					default answer ¬
					"" buttons {"Cancel", "Continue"} ¬
					default button ¬
					"Continue" cancel button ¬
					"Cancel" with hidden answer
				set {my_pass} to {quoted form of text returned} of result

				display dialog "Re-Enter password for " & ¬
					my_direction ¬
						with title ¬
					"AESCrypt - Re-enter Password" with icon 1 ¬
					default answer ¬
					"" buttons {"Cancel", "Continue"} ¬
					default button ¬
					"Continue" cancel button ¬
					"Cancel" with hidden answer
				set {my_pass2} to {quoted form of text returned} of result
				if my_pass ≠ my_pass2 then
					display dialog "Passwords did not match" with title "AESCrypt - password mismatch" buttons {"Close"}
					return
				end if
			end if
			
		on error number -128
			return
		end try
		
		set myPath to (path to me) as text
		set myAES to myPath & ":AESCrypt.app:Contents:MacOS:aescrypt"
		set myAES to quoted form of (POSIX path of myAES)
		
		if my_direction = "encryption" then
			do shell script (myAES & " -e -p " & my_pass & " " & tPath)
		else
			if outputMode = "Continue" then
				do shell script (myAES & " -d -p " & my_pass & " " & tPath)
			else
				-- We don't pipe to open since open will write a temp file to /tmp and not delete it.
				set clearFile to do shell script ("mktemp -t temp")
				do shell script (myAES & " -d -p " & my_pass & " -o  " & clearFile & " " & tPath)
				do shell script ("open -e " & clearFile)
				delay 5
				do shell script ("srm -f " & clearFile)
			end if
		end if
	on error errStr number errorNumber
		display dialog errStr & "(" & errorNumber & ")" with title "AESCrypt Error" buttons {"Close"}
	end try
end open

Post Reply