Re: Scripting Bridge and Finder selection
Re: Scripting Bridge and Finder selection
- Subject: Re: Scripting Bridge and Finder selection
- From: has <email@hidden>
- Date: Wed, 2 Jan 2008 12:54:52 +0000
Jjgod Jiang wrote:
How do I get the currently selected file in Finder via Scripting
Bridge
in Python? In Applescript, we have code like this:
tell application "Finder"
if selection is {} then
set finderSelection to folder of the front window as string
else
set finderSelection to selection as alias list
end if
end tell
but in Python, I tried
from ScriptingBridge import *
finder =
SBApplication.applicationWithBundleIdentifier_("com.apple.Finder")
selection = finder.selection()
however, selection is just a SBObject, which I couldn't get it's
real path, so how do I get it?
AppleScript and Scripting Bridge both have a fondness for behind-the-
scenes magic that obfuscates their actual mechanics in an attempt to
make them easier to use. In this case, the confusion is due to their
silent sending of 'get' events when certain conditions are met (what
AppleScripters call 'implicit gets').
The Scripting Bridge release notes has a section titled 'Lazy
Evaluation' which discusses the way that SB does this - IMO it's a lot
of needlessly confusing, misleading blather (a criticism that also
applies to SB itself), but if you're planning on using SB then go read
it anyway. Basically, what it boils down to is that SB only fires off
a 'get' event for you if the application dictionary declares the
property's type as a scalar (integer, string, etc.), list or record.
Finder's 'selection' property is of type 'specifier' (aka
'reference'), however, so in your case you need to send the 'get'
event yourself by calling the SBObject's 'get' method. AppleScript
uses a different set of rules for deciding when to send implicit gets,
which is why your original AppleScript code doesn't translate directly
across to SB.
As for specifying the requested type (keyAERequestedType), I don't
think SB provides a high-level way to do that, which means you'll have
to mess about with -sendEvent:id:parameters: and raw AE codes in order
to tell Finder to get the selection as a list of aliases.
In any case, my advice would be to ignore Scripting Bridge and go use
Python appscript instead (link is in my sig). It's a far more
polished, mature product and has much better dev tool support: a
powerful interactive built-in help system, HTML exporter for
application dictionaries (ASDictionary) and AppleScript-to-appscript
command translator (ASTranslate).
ASTranslate in particular is a great tool for beginners when learning
how to translate application commands between AppleScript and
appscript syntax. e.g. Take each Finder command from your original
AppleScript and feed it into ASTranslate to get its appscript
equivalent:
tell application "Finder"
if selection is {} then
end if
end tell
--> app(u'/System/Library/CoreServices/Finder.app').selection.get()
tell application "Finder"
set finderSelection to folder of the front window as string
end tell
--> app(u'/System/Library/CoreServices/
Finder.app').windows[1].folders.get(resulttype=k.string)
tell application "Finder"
set finderSelection to selection as alias list
end tell
--> app(u'/System/Library/CoreServices/
Finder.app').selection.get(resulttype=k.alias_list)
Once you know how to construct each individual command in Python, you
can manually translate the rest of your AppleScript code (variables,
conditionals, etc.) easily enough and put the whole lot together:
from appscript import *
Finder = app('Finder')
if Finder.selection.get() == []:
finderSelection = Finder.windows[1].folders.get(resulttype=k.string)
else:
finderSelection = Finder.selection.get(resulttype=k.alias_list)
Note, btw, that appscript only sends 'get' events when you tell it to;
this takes a little getting used to if you're coming from AppleScript,
but it's simple, easy to remember and completely consistent, and
you'll quickly get the hang of it.
HTH, and if you've any questions on py-appscript then the best place
to ask is on the PythonMac-SIG mailing list (http://mail.python.org/mailman/listinfo/pythonmac-sig
), or you can mail me directly if you prefer.
HTH
has
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden