Dragon Drop

I’m trying to figure out how to drop a text file to be read onto my X-code ASStudio project, but I’m starting to think it may not be possible. I looked at the Drag And Drop ASStudio example in mt "Developer folder, but it only shows how to drop onto a few window items after the app. is a;lready launched.
Is there a way to launch an ASStudio project from dropping a file onto its icon?

Greetings…

First, did you create a new “applescript droplet” application, or did you create it as a ‘document-based’ or plain AS application? Unless you know how to configure all of the document types and connections manually, there are things going on in a “droplet” app that the other types don’t come preconfigured to handle. Assuming that you’re using the droplet format, any code placed in the “on open” handler (inclued by default in the template) of the ‘file’s owner’ on open event will execute any time something is dropped on it’s application icon (like on the desktop or in the dock). At this point, all you have to do is write the code to get the file’s contents and set it to the contents of the text field.

j

Didn’t have the time to work out some sample code earlier, but here’s some that does the basic job…

on open names
	(* Get only the first file if multiple were dropped *)
	set theFile to item 1 of names
	
	(* Make sure it's a text file, and get the file's contents or set an error message *)
	tell application "Finder"
		set fileType to file type of theFile
	end tell
	
	if fileType is "TEXT" then
		set theContent to read theFile as string
	else
		set theContent to "The file was not a 'TEXT' file!"
	end if
	
	(* Display the results *)
	set contents of text view "contents" of scroll view "text" of window "window" to theContent
end open

This all assumes that you’ve started with a ‘droplet’ application, and that you have a scroll view and window with matching names to what I’ve used. Anything dropped on the applications icon will be evaluated, and then displayed if it is a “TEXT” file, according to it’s file type. You could also test by file extensions (i.e. that the file name contains “.txt”) or you could add a repeat loop searching for different types of raw “text” files, like html, cgi, java, databases, etc… from a list of acceptable file extensions or file types that you provide.

Good luck…
j

Oops!
I decided on the Droplet idea as an afterthought. I started the project as an “AppleScript application”. It’s pretty-much completed already except for the drop feature that I just decided I wanted. With the help of your post above, it shouldn’t be too difficult to transfer over what I have into a new AppleScript Droplet App.
I’m just making a Text-To-Speech-To-Aiff project with a bunch of unnecessary features to help me learn ASStudio. I still wish I would’ve recieved a response to my post on using a button to add text to a text field at the insertion point. I can’t believe I can only place text before, after, or completely replace the current text in a field. Oh well.
Thanks for your help. If I screw something up, I’ll be back. :slight_smile:

[url=http://bbs.applescript.net/viewtopic.php?p=28612#28612]http://bbs.applescript.net/viewtopic.php?p=28612#28612[/url]

Sorry, it just took a bit to figure some things out (or not)… 8)
j

Here’s that post again Jobu
If you anwered it already, you can just read it for pleasure :wink:

I used your code above replacing the names with my own.
Here is what I have:
on open names
set theFile to item 1 of names
tell application “Finder”
set fileType to file type of theFile
end tell
if fileType is “TEXT” then
set theContent to read theFile as string
else
set theContent to “The file was not a ‘TEXT’ file!”
end if
set contents of text view “view” of scroll view “view” of window “main” to theContent
end open
The icon accepts the drop, and the application opens, but all I see is my applications name in the menu. Where the heck is my main window? Am I supposed to make a connection somewhere in order to see the rest of my app?

That’s ok, we’re all human… :stuck_out_tongue:

When you create a droplet, there is no window in the default template, so I assume that you dragged one into your project. In the info window under “Attributes”, you’ll need to make sure the “Visible at launch time” box is checked so your window opens when the app starts. You might want to create a menu item in your main menu to reopen the window if a user closes it, so you won’t have to quit and restart to get the window open again. :wink:

on choose menu item theObject
	if name of theObject is "showMainWindow" then
		show window "main"
	end if
end choose menu item

Your idea with the image view sounds ok, but I’m not sure the mouse events work that well for image views. A second button beneath the first might be more reliable, but I question the results of a “fast-moving” mouse enter, and whether it might not register under certain circumstances.

I’m still looking for answers, and hopefully someone will help solve this dilemma.

Cheers,
j

Ok, Now I feel stupid. I have a window now. Thank you.
One more little prob (of course)…
The text view doesn’t set to the text of the dropped item.
I used the code that I posted above, and I’m sure I have everything named correctly.
I tried pasting
‘Tell application “finder”
display dialog “test”
end tell’
in several places to see where the problem is, but I can’t even get that to show.
What is the “names” variable after the on open handler? Am I supposed to set that as anything or make a property for it? I also tried “open theFile for access” before ‘set theContent to read theFile as string’, but that didn’t work either.
-G

The ‘names’ variable is a list of the file paths that is automatically sent to the script when files are dropped on the icon. In this case you don’t want to mess with that. I don’t see anything else wrong with your code, in fact it is verbatim from what I posted other than the names of the text/scroll/window names. So, check those again. If they’re fine, then check to make sure that ‘File’s Owner > Application > open’ event is actually connected to the script ‘on open’ handler. Another thought I had was to make sure that you are actually dropping files with a “TEXT” file type on the app. To see if this is the problem try…

on open names
	set theFile to item 1 of names

	try
		set theContent to read theFile as string
	on error
		set theContent to "Could not read file!"
	end try
	set contents of text view "view" of scroll view "view" of window "main" to theContent
end open

If that produces nothing try the following test codes to see what is being passed to the script via the open event, what is in the names variable, and whether your connections/names are bad or corrupt.

on open names
	set contents of text view "view" of scroll view "view" of window "main" to (names as string)
end open

-- OR --

on open names
	set contents of text view "view" of scroll view "view" of window "main" to "Test"
end open

Sorry, since it’s working on my end I can’t offer much more advice. Good luck…
j

I got it to work after starting from scratch. This time I just imported a normal window into the project. I tried using a window/panel in the other one. I don’t know why that would matter, but I can still place a panel in later.
Thanks again for your time n’ knowledge
-G

Give this a try…
Check ‘Has Texture’ for the window. I rebuilt my project about 10 times and alternated checking this box. When it was checked the app crashed. when it’s unchecked, it works perfectly. Weird hey?
-G

You can change this pretty easy in Xcode 2.1:

Click the arrow next to Targets and double click your Target, go to Properties, and down at the bottom, you’ll notice “Document Types” is blank…

Click the plus (+)

Rename it “DocumentTypes”, change the Extensions to what you want, (i used **** so all files work) and change OS Types to “****” (without quotations), then scroll to the back and change Store Type to “Binary” and check the checkbox under “Package”. voila…it should work. (at least it darkens when i hold a file over it…i’m going to add my code soon)

EDIT: Also, don’t forget to go into the applescript pane of Files Owner in Interface Builder and check “Open” under Application, and then check YourScriptsName.Applescript

Oh and sorry mods for ressurecting a dead thread, but I searched for doing this, and all I could find was this, so I posted an update :x