MacPython applet problem

boris m bm007 at gmx.de
Sat May 15 13:24:01 EDT 2004


After some tinkering with AppleScript, I found a workaround for my
problem. I think it is worth sharing, so here we go...

As Ronald pointed out, python applets currently do not support
input/output functions like 'input' or 'print'. Of course, I could run
the script from the command line, but then I would not have a nice
drag and drop feature.

I wanted to have both drag/drop (via sys.argv) *and* input/output
functions. So I wrote an AppleScript droplet that allows me to do
this. The droplet is currently limited to accept just one item, but it
could easily be extended.

This was one of my first explorations into AppleScript, so I am more
than open to suggestions and improvements.

Boris 

---

--> save as application
on open dropItem
	set itemPath to POSIX path of dropItem
	set itemPathStr to (itemPath) as string
	set itemPathCLI to textReplace(itemPathStr, " ", "\\ ")
	
	tell application "Finder"
		set currentMacFolder to (container of (path to me)) as alias
		set currentFolder to POSIX path of currentMacFolder
		set folderPath to (currentFolder) as string
	end tell
	
	set folderPathCLI to textReplace(folderPath, " ", "\\ ")
	--> don't forget chmod +x myscript.py
	--> myscript.py is in the same directory as the droplet
	set scriptPath to folderPathCLI & "myscript.py" & " " & itemPathCLI
	
	activate application "Terminal"
	tell application "Terminal"
		do script scriptPath
	end tell
end open



on textReplace(theText, srchStrng, replStrng)
	tell (a reference to AppleScript's text item delimiters)
		set {od, contents} to {contents, {srchStrng}}
		try
			set {textList, contents} to {(text items of theText), {replStrng}}
			set {newText, contents} to {(textList as text), od}
			return item 1 of result
		on error errMsg number errNbr
			set contents to od
			error errMsg number errNbr
		end try
	end tell
end textReplace




>> My problem is that I the script needs some user input (I want to
drag
>> some
>> text files to the applet and then specify some changes). For the
user
>> input
>> I am using:
>> 
>> inputStr  = raw_input("Enter a number: \n>>> ")
>> 
>> This, however, seems to produce the error.
> 
> That's right. I'm pretty sure applets on OSX do no (yet) support user 
> input/output using the input function and print statement.
> 
> IIRC this is on the TODO list for the next release of MacPython.
> 
> B.T.W. with MacPython the default action for .py file is to execute 
> them in a Terminal window, which gives you complete support for input 
> and print.
> 
> Ronald



More information about the Python-list mailing list