Python and AppleScript
Python and AppleScript
- Subject: Python and AppleScript
- From: David Hood <email@hidden>
- Date: Tue, 18 Mar 2003 16:12:28 +1200
On Tuesday, March 18, 2003, at 08:11 AM, Paul Berkowitz wrote:
I decided to opt first for Python, because it seems just
as powerful and much more readable and approachable. Its syntax,
although
rather leaner, is even quite similar to AppleScript's.
I too opted for python rather than perl in picking up a command line
scripting language. Because I tend to pick up things from example, when
I do a web search for some example python code I get something I find
relatively readable and can quickly get a sense of it's purpose.
Looking at example perl code I stumble across often leaves me feeling
like it was written by someone banging their head against the keyboard.
I just get put off by the density of the syntax.
For the general good here is an example of some python script inside an
Applescript. Admittedly, all it does is read in a selected file, but it
shows one way such scripts can be written (by changing ptext to the
desired python script).
set nl to (ASCII character 10) --unix newline ending
set cr to (ASCII character 13) --mac newline ending
set bq to "\"" --backslashed doublequote for readibility
set anyoldfile to choose file
set unixFriendly to quoted form of POSIX path of anyoldfile
set ptext to "f=open(" & unixFriendly & ",'r')
something=f.read()
f.close()
print something"
set ptext to newImprovedText(ptext, cr, nl)
set pythonCommand to "python -c " & bq & ptext & bq
set pythonOutput to do shell script pythonCommand
return pythonOutput
--for correcting line endings
on newImprovedText(offeredText, Fromsomething, Tosomething)
set saveddelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to Fromsomething
set listOfText to every text item in offeredText
set AppleScript's text item delimiters to Tosomething
set newString to listOfText as string
set AppleScript's text item delimiters to saveddelimiters
return newString
end newImprovedText
Regards,
David Hood
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.