Shell script to get path of application in MacOS

0

Posted by Jaime | Posted in Tip | Posted on 10-08-2011

Tags: , , ,

Here is a shell script that finds an application on my hard drive using a nifty call to Finder using AppleScript:

#!/bin/sh
 
result=`exec osascript <<EOF
tell application "Finder"
	set thePath to (POSIX path of (path to application "Doxygen"))
end tell
EOF`
 
echo $result

Running AppleScript commands from the Terminal.app

1

Posted by Jaime | Posted in Coding, Tip | Posted on 30-06-2011

Tags: , ,

I found some pages on the web on how to run an AppleScript from the terminal, but this seems to be the only one that works in my situation:

?View Code APPLESCRIPT
echo 'tell application "Google Chrome" to quit' | /usr/bin/osascript

In my case, my keyboard stopped working so I wanted to safely shut down the programs via ssh before I rebooted the workstation.

Adobe Scripting Additions

0

Posted by Jaime | Posted in Coding, What's New | Posted on 20-06-2011

Tags: , ,

I’ve been getting warning messages in my Console log stating the “Adobe Unit Types.osax” has deprecated functions.

Then, when I starting scripting Xcode with AppleScript, I’ve been getting a bunch of errors about the above listed library.

Well, Adobe had something to say about that: http://kb2.adobe.com/cps/516/cpsid_51615.html

Print to PDF with AppleScript and Microsoft Word

0

Posted by Jaime | Posted in Coding | Posted on 05-04-2009

Tags: , , ,

Recently, I needed to automate printing a Word document to a PDF file and have it saved to a predefined location. There were some good articles on the web about how to do the printing part, or the PDF part but not how to save the output.

So, here’s my stab at printing a Word document to a PDF file in a known location:

?View Code APPLESCRIPT
-- This AppleScript will open up our Word doc and save it as a PDF
 
set myDoc to "/Users/jaimerios/Documents/Developer notes.doc"
set pdfSavePath to "/Users/jaimerios/Documents/PDFs/"
 
tell application "Microsoft Word"
	activate
	open myDoc
end tell
 
tell application "System Events"
	tell process "Microsoft Word"
		-- Press command+p to open our print dialog
		keystroke "p" using command down
 
		-- Let's make sure our print dialog is up
		repeat until exists window "Print"
		end repeat
 
		-- Click the PDF menu button
		click menu button "PDF" of window "Print"
 
		-- Make sure the menu is up
		repeat until exists menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
		end repeat
		-- Select the "Save as PDF" menu item
		click menu item "Save as PDF…" of menu 1 of menu button "PDF" of window "Print"
 
		-- Make sure the save dialog is visible
		repeat until exists window "Save"
		end repeat
 
		-- Press command+shift+g to show the "Go" drop down sheet
		keystroke "g" using {command down, shift down}
		-- Set our location field to our pdfSavePath
		set value of text field 1 of sheet of window "Save" to pdfSavePath
		-- Now click the Go button
		click button "Go" of sheet of window "Save"
 
		-- Now that we are in our desired folder, set the file name and save
		set value of text field 1 of window "Save" to "Special Developer Notes.pdf"
 
		click button "Save" of window "Save"
	end tell
end tell

Now the only limitation to this script is that it doesn’t handle the condition where the PDF file might already exist in our output location.

I have to give credit to the original author who came up with tis script. Their AppleScript code is located at this website link.

Well, I hope this helps you and if you have any comments to make this script better, let me know.

Happy coding!

Decrypting email using GPG and AppleScript

2

Posted by Jaime | Posted in Coding | Posted on 11-02-2009

Tags: , , , ,

In a previous posting, I had listing all of the applications you would need to decrypt and encrypt email messages in Apple Mail. However, I did find that after moving up to Mac OS X 10.5.6, I was unable to decrypt messages delivered from certain mail servers. I could decrypt the messages by hand, but this was a real pain to do over and over.

So this is a perfect opportunity to leverage AppleScript!

The script on this page which was actually inspired from other scripts in the “AppleScript Examples” folder in the Applications folder. The script gets the contents of the currently selected email message and creates a temp file in the temp folder path. The script then uses the Terminal.app application to invoke gpg which is installed when you install the “GNU Privacy Guard” application, and then displays the message in TextEdit. Not an elegant solution, but it gets the job done.

?View Code APPLESCRIPT
 
set passphrase to "MySuperSecretPassphrase"
set randNumber to ((random number) as string)
set tempPath to ((path to temporary items from user domain) as string)
 
set encryptedFP to tempPath & "EncryptedMessage" & randNumber & ".eml"
set decryptedFP to tempPath & "DecyptedMessage" & randNumber & ".txt"
 
tell application "Mail"
    set selectedMessages to selection
    if (count of selectedMessages) is equal to 0 then
        display alert "No Messages Selected" message "Select the message you want to get the raw source of before running this script"
    else
        set theMessage to item 1 of selectedMessages
        set theSource to source of theMessage
        set theFileID to open for access file encryptedFP with write permission
        set eof of theFileID to 0 -- in case there happens to be a temporary file with this name already, wipe it out
 
        write theSource to theFileID
        close access theFileID
        set encryptedFP to POSIX path of encryptedFP -- convert colons to forwardslashes
        set decryptedFP to POSIX path of decryptedFP
    end if
end tell
 
tell application "Terminal"
    activate
    with timeout of 1800 seconds
        do script with command "echo '" & passphrase & "' | gpg  --passphrase-fd 0 -v --output " & decryptedFP & " --decrypt " & encryptedFP
    end timeout
end tell
 
delay (1) -- Let's give the Terminal.app some time to work
 
tell application "Finder"
    set fileToOpen to tempPath & "DecyptedMessage" & randNumber & ".txt"
    if exists file fileToOpen then
        tell application "TextEdit"
            activate
            open fileToOpen
        end tell
 
        tell application "Terminal"
            quit
        end tell
    else
        display dialog "Couldn't find file " & decryptedFP
        open folder tempPath
    end if
end tell

If you happen upon this page because you had the same problems that I did, then I hope this script helps you out.