[Pythonmac-SIG] Any way to use Python scripts (instead of Applescript) for folder actions?

has hengist.podd at virgin.net
Wed Jun 2 09:46:08 EDT 2004


Kenneth McDonald wrote:

>Or, I guess more accurately, is there any _easy_ way to do this.

Not really. It requires two things we don't currently have: OSA 
support in MacPython, and better OSA support in Cocoa[1].


>I'm  sure that if I know Applescript I could come up with an 
>Applescript that would run a Python script but, I don't want to 
>learn AppleScript--my brain is  already full of languages!

It's a PITA to learn anyway; slow, bug-laden, and riddled with design 
flaws. (Some core aspects are quite nice; e.g. Smalltalk-style 
messaging and built-in object persistence, and a very neat, if 
underpowered, prototype-based object system that I much prefer over 
Python's class-based bottomless pit. But mostly it's a mess.)


Best thing is to write an AppleScript wrapper that runs a shell 
script upon receiving folder events. Here's one I knocked together:

----------------------------------------------------------------------

-- PROPERTIES

property shellScriptPath : "/path/to/shell/script" -- YOUR PATH HERE

-- SUPPORT

on _doScript(folderAction, folderAlias, args)
	try
		do shell script ¬
			(quoted form of my shellScriptPath) & space & ¬
			quoted form of folderAction & space & ¬
			quoted form of POSIX path of folderAlias & space & ¬
			args
	on error eMsg number eNum
		beep 2
		tell application "Finder"
			activate
			display dialog eMsg & return & eNum with icon stop
		end tell
	end try
end _doScript

on _escAliasList(lst)
	set str to ""
	repeat with aliasRef in lst
		set str to str & quoted form of POSIX path of aliasRef & space
	end repeat
	return str
end _escAliasList

-- EVENT HANDLERS

on opening folder folderAlias
	_doScript("opening folder", folderAlias, "")
end opening folder

on closing folder window for folderAlias
	_doScript("closing folder window", folderAlias, "")
end closing folder window for

on moving folder window for folderAlias from boundingRect
	set AppleScript's text item delimiters to space
	_doScript("moving folder window", folderAlias, boundingRect as string)
end moving folder window for

on adding folder items to folderAlias after receiving aliasList
	_doScript("adding folder items", folderAlias, _escAliasList(aliasList))
end adding folder items to

on removing folder items from folderAlias after losing aliasList
	_doScript("removing folder items", folderAlias, 
_escAliasList(aliasList))
end removing folder items from

----------------------------------------------------------------------


>More generally, is there any way to hook Python into the system as 
>one can do with AppleScript, _without_ need to venture (far) outside 
>of Python?

AppleScripts don't directly hook into the system; they're attached to 
apps like System Events that translate application/system-level 
events into Apple events that they fire at those attached scripts to 
trigger their handlers. Like I say, the only reason Perl, Python, 
etc. can't be used in place of AppleScript here is that folk [on both 
sides] haven't yet done the OSA support needed.

MacPython's application scripting support is a bit further on [though 
I really need help to complete it in time for the MacPython 2.4 
release; hint-hint]; see:

<http://freespace.virgin.net/hamish.sanderson/appscript.html>

HTH

has

[1] Its current offering, NSAppleScript, only supports AppleScripts, 
which is no good if you want to attach scripts written in other OSA 
languages to Cocoa apps, of which System Events is one.
-- 
http://freespace.virgin.net/hamish.sanderson/



More information about the Pythonmac-SIG mailing list