Problem with a tell block. Word opens instead of textedit

What is wrong with the following applescript…I added the final end if and end repeat just to make the code complete…

But anyway… I get to the point of choosing the directory…it works… It then proceeds to open the files with WORD…not Textedit.
In a version of the code that I have that does not attempt to get all files in all subfolders the files open just fine using textedit. What gives?


	set inpath to "iv:Users:ivsimler:Desktop:Test:"
set pdfextension to ".pdf"
set badpath to "iv:Users:ivsimler:Desktop:"
set outpath to "IV:Users:ivsimler:Desktop:Destination Test Folder:"



--type for word files is "msng"

--set item_list to list folder inpath without invisibles 

set inpath to choose folder
tell application "Finder"
	set the file_list to files of entire contents of inpath
end tell
repeat with print_item in file_list
	
	if name extension of print_item is "doc" then
		tell application "TextEdit"
			open print_item
			print print_item with nodialog
			close window
			delay 5
		end tell
      end if
end repeat

Hi, nukular.

I’d guess it’s because file_list contains Finder references to the files, so the open command is being directed to the Finder even though it’s in a tell block for TextEdit. The Finder will open doc files with the default application for those.

Try setting file_list to a list of aliases:

tell application "Finder"
	try
		set the file_list to (files of entire contents of inpath) as alias list
	on error
		set the file_list to {(first file of entire contents of inpath) as alias}
	end try
end tell

You’ll then have to use the Finder again, or info for, to get the name extensions. I don’t know what’s in the rest of your script, but if you’re only interested in files with “doc” extensions, you can include a filter when you tell the Finder to get the files:

tell application "Finder"
	try
		set the file_list to (files of entire contents of inpath whose name extension is "doc") as alias list
	on error
		set the file_list to {(first file of entire contents of inpath whose name extension is "doc") as alias}
	end try
end tell

Am I missing something here? There used to be another post here and it disappeared…I guess he deleted it.

I used the other post and its a little hard to reply now… but anyhoo…

In the rest of my script I need to simply use the name of the file I extracted…not the complete path to the file… How can I extract this from a list created using

tell application “Finder”
set the file_list to files of entire contents of inpath
end tell

the “file_list” variable is now a complete list of every file in the folder…along with path… The problem is the printing I do with Textedit is putting the output file on the desktop… with a .pdf added to the end so I was using a statement like:

set BADPATH_FILE to badpath & print_item_file & pdfextension.

But print_item is a complete directory path to the item (as well as the file name I need) I referenced from “file_list”

Hi, nukular.

I’ve now had time to look at this a little more. The ‘whose’ filter I suggested works, but is quite slow. It’s much faster to coerce the folder’s ‘entire contents’ to Unicode text using a carriage return or line feed delimiter and to get the paragraphs of the result. This gives a list of the paths of all the files in the folder and its subfolders. You then loop through the list looking for any paths that end with “.doc”.

When you open a file in TextEdit (I presume you can’t do what you want with Word for some reason), the name of the front document is the same as the name of the file.

set outpath to (path to desktop as Unicode text) & "Destination Test Folder:"

-- Choose a folder and get the path of every file in its hierarchy.
set inpath to (choose folder)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
tell application "Finder"
	set the file_list to paragraphs of ((files of entire contents of inpath) as Unicode text)
end tell
set AppleScript's text item delimiters to astid

repeat with print_item in file_list
	-- If a path ends with ".doc", open the associated file in TextEdit.
	if (print_item ends with ".doc") then
		tell application "TextEdit"
			open file print_item
			-- The name of the front document is the name of the file.
			set file_name to name of front document
			set pdf_name to (text 1 thru -5 of file_name) & ".pdf"
			
			print front document without print dialog
			close front window
			delay 5
		end tell
	end if
end repeat

I’m assuming for the moment that you have your own arrangements for saving the document as a PDF file. I’ve been looking today at GUI Scripting the “Save As PDF.” function under the “PDF” button in the “Print” dialog sheet. It’s possible to write a series of clicks and keystrokes to navigate through the “Save” dialog, but it’s a bit grotesque to watch. A slightly more elegant way, if you have a fixed destination folder in mind, is to put together an Automator workflow and place it in a folder called “PDF Services” in your user or local Library folder. It’ll then appear in the “PDF” button menu of “Print” dialogs and can be selected instead of the “Save As PDF.” item. The workflow I created to test the idea (and the script based on it) consists of the “Rename Finder Items” preset action followed by the “New Folder” one. The first is set up to change “.doc.pdf” in item names to just “.pdf” and the second then routes the items through to a folder called “Destination Test Folder” on the Desktop. The workflow, saved under the name “Save PDF to Destination Test Folder.workflow”, appears second-from-bottom in the “PDF” button menu, where it’s quite easy to access with GUI Scripting. Let me know if you need any sample code for this.