Can't get document after drag and drop script opens it

Can someone tell me why this doesn’t work. I created a drag & drop, but after I drag the file, it opens, and then nothing happens.


on open some_items
	repeat with this_item in some_items
		try
			tell application "Finder"
				open document (this_item)
			end tell
		end try
	end repeat
end open
set theText to text of document 1 of application "TextEdit"
tell application "TextEdit"
	close document 1
	make new document
end tell
tell application "TextEdit"
	set text of document 1 of application "TextEdit" to theText
end tell

Assuming that you realize that you are going to get all the formatting info which looks like this:

"{\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf330 {\\fonttbl\\f0\\fnil\\fcharset77 LucidaGrande-Bold;\\f1\\fnil\\fcharset77 LucidaGrande;\\f2\\fswiss\\fcharset77 Arial-BoldMT; \\f3\\fswiss\\fcharset77 ArialMT;}
you get text from a file like this:

set f to choose file
set t to read f

I understand

set f to choose file

I don’t understand

set t to read f

When I use

set a to choose file
tell application "TextEdit" to open a

a window appears and I choose the file and everything runs fine. I don’t want to have a menu pop up asking me to choose a file. What I would like to happen is to drag a file on to the drag and drop icon, and the script open the file and perform the task I have assigned it. So far, it opens the file, and then nothing happens.
In the grand scheme of things, I would like to drop a lot of files on the drag and drop script and have it perform the assigned task to each and every one of them.

I can perform the drag and drop with automator, and then run the applescript, so surely there is a script out there to do the same thing.

Okay, I figured out

set t to read f

What I don’t have hardwired into the script is the name of the text file I want to access , because it needs to work on many files. I am relatively new to applescript so I get kind of confused on variables. Is there some way, after doing a drag and drop to open the file, to set a name variable to the opened file so I can call that file up without knowing its real name.
Opening a window and choosing the file works, but it is an extra step that will be multiplied by 192 files.

I have all this worked out in automator, but nice, applescript would be much quicker. The Automator file runs in about 4 seconds. The applescript runs in about two after I choose the file.

right click any .txt file … click “get info” … choose “opens with” select your script (saved as application - run only" ) and click “change all…”

you now get ALL .txt files opening with you applescript
however how it gets the file name I dont know - hope someone else can help there - I’m trying to do
same thing here…
http://bbs.applescript.net/viewtopic.php?id=17238
…if you get any joy please post a link onto my question - good luck :slight_smile:

it seems not too many people helping on this site :frowning:
home my swap from windoze to mac wasnt a bad choice

Hi Skip,

I’m using an older version of TextEdit, but it may be the same on your version. The open command from the dictionary:

open: Open an object.
open a list of alias – list of objects

It needs a list! If you tell TextEdit to open with a reference to a list item, then it errors. What I did is send it exactly what it wants which is a list of alias references.

Before you create a droplet it’s a good idea to make the script work first in the Script Editor or whatever. What I do is either use the Finder’s selection or put some files in a folder and get every item as alias list. Here’s an example before making it a droplet:


set f to choose folder
tell application "Finder"
	set some_items to every file of f as alias list
end tell
tell application "TextEdit"
	launch
	activate
end tell
repeat with this_item in some_items
	tell application "TextEdit"
		open (this_item as list)
		set t to text of front document
		close front document
		set d to (make new document at front)
		set text of d to t
	end tell
end repeat

Here the repeat variable this_item is a reference to a list item. Another way to do this is to get the contents of the reference to the list item. For example:


set f to choose folder
tell application "Finder"
	set some_items to every file of f as alias list
end tell
tell application "TextEdit"
	launch
	activate
end tell
repeat with this_item in some_items
	set c to contents of this_item
	tell application "TextEdit"
		open c
		set t to text of front document
		close front document
		set d to (make new document at front)
		set text of d to t
	end tell
end repeat

What this does is get the value of the list item which is an alias reference. Here TextEdit allows a non-list of alias, because it is an actual alias reference which is the value of the list item.

Now that we got it working in the Script Editor, you can change it to a droplet.


on open some_items
	tell application "TextEdit"
		launch
		activate
	end tell
	repeat with this_item in some_items
		tell application "TextEdit"
			open {this_item}
			set t to text of front document
			close front document
			set d to (make new document at front)
			set text of d to t
		end tell
	end repeat
end open

Note that getting the text of an rtf document in TextEdit, returns text. If you have your TextEdit preferences set to plain text, then this is one way to convert rtf to plain text.

I think this is how it goes for the most part.

gl,