ASObjC with scriptable application (ScriptingBridge)

The code itself is properly not so useful… The code is more to show
how the framework ScriptingBridge works if you like to script ASObjC with scriptable application.
In this example I use Finder to get information from files in trash.

The code is only for research and for my curiosity.

use framework "Foundation"
use framework "ScriptingBridge"
use scripting additions

set bundleID to id of application "Finder"
set finder to current application's SBApplication's applicationWithBundleIdentifier:bundleID

tell finder to tell |trash|() to tell |items|()
	log (its valueForKey:"URL") as list
end tell

tell finder to set theURL to (its valueForKey:"URL") of |items|() of |trash|() as list
log theURL

set theURL to ((its valueForKey:"URL") of |items|() of |trash|() of finder) as list
log theURL

tell finder to tell |trash|()
	log (its |properties|()) as record
	tell |items|()
		log (its valueForKey:"name") as list
		log (its valueForKey:"URL") as list
		log (its valueForKey:"comment") as list
		log (its valueForKey:"kind") as list
		log (its valueForKey:"nameExtension") as list
		log (its valueForKey:"modificationDate") as list
		log (its valueForKey:"creationDate") as list
		log (its valueForKey:"owner") as list
	end tell
end tell

use framework "Foundation"
use framework "ScriptingBridge"
use scripting additions

tell application "System Events" to set thePID to unix id of application process "Finder"
set finder to current application's SBApplication's applicationWithProcessIdentifier:thePID

tell finder to tell |trash|() to tell |items|()
	log (its valueForKey:"name") as list
end tell

set thePath to POSIX path of (path to applications folder) & ""
set theURL to current application's |NSURL|'s fileURLWithPath:thePath
set textedit to current application's SBApplication's applicationWithURL:theURL

tell textedit to tell |documents|()
	log (its valueForKey:"path") as list
	log (its valueForKey:"name") as list
	log ((its valueForKey:"text")'s objectAtIndex:1) as list
	log (its valueForKey:"properties") as list
end tell

Here is a example using PyObjC

Why would you ever do this?
Makes no sense.

The number of Apple Events that will get sent back and forth will make
All of this extremely slow

The purpose of the script was only to understand the framework… (Scripting Bridge)

AS already knows how to talk to the terminology of scriptable application so in that sense you are
right but that was never the purpose of the example.

Sorry if I was not clear enough.

It’s worth understanding the purpose of the framework. Initially, the only way for an app written in Objective-C to call send and receive Apple events was to create a script, call that, and do the necessary value conversions. ScriptingBridge was created as a way for Objective-C code to call Apple events directly, without AppleScript.

So calling it from AppleScript is, well, somewhat circular.

Yeah I think it would be more helpful to post the Objective-C code.

I started with AppleScript the user more AsObjC,
The moved into using XCode and started in Objective-C.
I mainly use Objective-C but much of my programming is based around DJing and music so
I interact a great deal with Spotify and iTunes. I’ve used the ITLibrary framework and
It’s great, but it doesn’t allow you to manipulate anything. Also I’ve found that the searching is
Still not up to what I like (I’m just starting to learn how use RegEx’s in my predicates). And that
The iTunes scripting search give me better results.

So over time I’ve been trying to use more scripting bridge than AppleScripts
But it’s defintely not as easy to use and the results are not always what expected.
Also definitely some time issues with sending Apple Events.

Check this PyObjC script you could see its almost the same as my ASObjC code in my first post.

To run below code use the Python that was included with your Mac ex. /usr/bin/python
If you use Python3 you need to change the print line to be: print(iTunes.currentTrack().name())
And install the package PyObjC

I’m not saying this approach is fast but it does work.