call python from applescript?

I administer a Google Apps domain and came across GAM, an excellent command line / Python tool for automating admin operations:

http://code.google.com/p/google-apps-manager/

This can be used for creating google calendar resources (rooms, facilities, etc) without endless clicking in the GUI.
Theoretically it should be possible to do batch operations by means of an AppleScript that steps through a .csv file and tells GAM to create the items specified.

My first attempt looks like this:

--set csvEntries to paragraphs of (display dialog "Paste .csv text here" default answer "")
set csvEntries to "dance-studio-small,Dance Studio small,room/facility,102"

repeat with i from 1 to (count paragraphs of csvEntries)
	set AppleScript's text item delimiters to ","
	set {theID, theName, theDescrip, theType} to text items of (csvEntries's paragraph i)
	set AppleScript's text item delimiters to {""}
	do shell script "cd ~/gam
	python gam.py create resource " & theID & " name " & quoted form of theName & " description " & quoted form of theDescrip & " type " & quoted form of theType
end repeat

Needless to say, it doesn’t work or I wouldn’t be posting this .

  1. GAM works fine manually in Terminal. You have to first navigate to the directory where the gam.py file lives, in my case a folder called “gam” in my homedir. For example:

cd ~/gam
python gam.py create resource dance-studio ‘Dance Studio’

But what is the syntax to make that work in AppleScript with “do shell script”?

  1. I resorted to hard-coding the csv text into my script during testing, or using “display dialog” to ask for a pasted csv list, but obviously it would be way less clunky to have the script actually read the data from a delimited text file. How to do this?

Thanks in advance everyone -

Hi,

  1. eiher write cd /path/to/dir; command or you specify the full path of the executable

set csvEntries to "dance-studio-small,Dance Studio small,room/facility,102"
set gamExecutablePath to POSIX path of (path to home folder) & "gam/gam.py"
repeat with i from 1 to (count paragraphs of csvEntries)
	set AppleScript's text item delimiters to ","
	set {theID, theName, theDescrip, theType} to text items of (csvEntries's paragraph i)
	set AppleScript's text item delimiters to {""}
	do shell script "/usr/bin/python " & quoted form of gamExecutablePath & " create resource " & theID & " name " & quoted form of theName & " description " & quoted form of theDescrip & " type " & quoted form of theType
end repeat

  1. you can read a text file with

set theText to read file "disk:path:to:file.txt"

included in your script


set csvParagraphs to paragraphs of (read file "disk:path:to:file.txt")
set gamExecutablePath to POSIX path of (path to home folder) & "gam/gam.py"
set AppleScript's text item delimiters to ","
repeat with oneParagraph in csvParagraphs
	set {theID, theName, theDescrip, theType} to text items of oneParagraph
	do shell script "/usr/bin/python " & quoted form of gamExecutablePath & " create resource " & theID & " name " & quoted form of theName & " description " & quoted form of theDescrip & " type " & quoted form of theType
end repeat
set AppleScript's text item delimiters to {""}

note: as the shell script doesn’t concatenate text items, you can put the text item delimiters changes outside the repeat loop

try this…

set csvEntries to "dance-studio-small,Dance Studio small,room/facility,102"

set csvPararaphs to paragraphs of csvEntries
set gamPath to (path to home folder as text) & "gam:gam.py"

set AppleScript's text item delimiters to ","
repeat with i from 1 to count of csvPararaphs
	set {theID, theName, theDescrip, theType} to text items of (item i of csvPararaphs)
	do shell script "/usr/bin/python " & quoted form of POSIX path of gamPath & " create resource " & theID & " name " & quoted form of theName & " description " & quoted form of theDescrip & " type " & quoted form of theType
end repeat
set AppleScript's text item delimiters to ""

EDIT:
Oooops, StefanK beat me to it. We did basically the same thing. :smiley: