Developer--> Examples--> AppleScript

Dear sirs, I installed the latest XCode version, looking around the Forums I found many references to some examples (that should be placed in the Developer–>Examples folder), but can’t find any AppleScript examples.

In particular, I’d like to better understand how to make a drop zone (area) inside my Cocoa-AppleScript Application, so for what I can see there should be a project upon this subject.

Googling a little bit, for what I can understand, the so called AppleScript Studio (and many of its function calls) have been substituted by the Objective-C. Maybe the AppleScript Studio examples were old, and didn’t work under SnowLeopard (but can’t figure it out).

Can anybody please explain (or link me :slight_smile: to some informations)?

Thanks in advance

Giovanni

That’s more or less it. AppleScript Studio is deprecated in 10.6, and sadly there are no examples of AppleScript ObjC installed. A few samples have been put together by third parties, and most of them have been referenced in this forum.

Thanks Shane, do you know if there is any Objective-C Drop Area working example in this forum?

Do you mean enabling drag-and-drop? If so, yes.

I saw a (thread http://macscripter.net/viewtopic.php?id=31823) containing a quick tutorial to transform a xcodeproj into a droplet (but I still have a doubt: how to allow any document type? I tried to put ‘****’ and other things in the Extension Field but didn’t result in what I want). I would like to be able to drop on the application icon also folders if possible, which is the better way?

By the way I’m interested in enabling the main application’s window to receive (into a well defined box) dropped files and/or folders, and obtain their path. In particular I’d like to understand if I have to build a class (using Cocoa), or if I can do it completely through AppleScript.

Thanks

Giovanni

That looks like something for AppleScript Studio, not AppleScriptObjC. In ASObjC you need to add an application_openFiles_(theApp, fileList) handler to your application delegate. Then you need to choose Edit Active Target “” from the Project menu, and clicking on the Properties tab. At the bottom is a section headed Document Types. To add a type of document you want your app to recognise, and hence accept via drag-and-drop, click the + button at the bottom. An extension of * should do the trick; make it a Viewer rather than an Editor.

I’m pretty sure that’s been covered in a post here already.

Thanks Shane, found out how to transform a project in a droplet (that can read any kind of files/folders).

I tried to find the thread you said (the one describing a working AppleScriptObjective-C drop area, such as NSView or the Window itself), but can’t find the right handler to call the “drop event”.
In Interface Builder I made the Window Outlet to Delegate the Script, and also added a property binding My IB Window and the script.

property MyWndw: missing value 

Does anybody know if I’m wrong (or missing something), and what handler should I use to get the path of a file and/or folder dropped inside the Main Window of the program?

Thanks in advance

Giovanni Medici

First you have to register your window for drag an drop, probably in applicationWillFinishLaunching_

on applicationWillFinishLaunching_(aNotification)
	-- Register for drag and drop 
	mainWindow's registerForDraggedTypes_({"public.file-url"})
end applicationWillFinishLaunching_

The you need to make your script the delegate of the window, and include at least two handlers. If you’re going to accept any files, they should be something like this:

on draggingEntered_(sender)
	set pb to sender's draggingPasteboard()
        set theOptions to {NSPasteboardURLReadingFileURLsOnlyKey:1}
	return pb's canReadObjectForClasses_options_({current application's|NSURL|}, theOptions)
end draggingEntered_

on performDragOperation_(sender)
	-- Get the file path
	set pb to sender's draggingPasteboard()
        set theOptions to {NSPasteboardURLReadingFileURLsOnlyKey:1}
	set imageURLs to pb's readObjectsForClasses_options_({current application's |NSURL|}, theOptions)
	set draggedURL to item 1 of (imageURLs as list)
	set filePath to draggedURL's |path|()
	return true -- otherwise it doesn't happen
end performDragOperation_

Thanks Shane, you really helped a lot. Unfortunately I didn’t find out how to enable for dragging an NSImageView too, but I think that my test program works decently, even if without an Image to tell the user he could also drop files…
http://giovannimedici.altervista.org/Giovanni_Medici/Search_%26_Replace.html

This is the url of the page where one can download the (in?!)utility I made!

Basically it works as a search and replace routine on user specified files (files type, extensions) and regular text or regular expressions can be used. The user might choose also to overwrite original files or save them in another folder.

I made it just for “tutorial” purposes but kind of like it because I had to change few links on a bunch of files (some hundreds, website…) and did it in few seconds.

This link sends to the zipped project.

Please use this program carefully since it actually can overwrite (original files).

http://giovannimedici.altervista.org/ccount/click.php?id=3

The problem I’ve had approaching droplets in various forms is that there seems to be a few ways to go about the actual action.

I’ve loved ShinyDroplets (free and open source on github)