Scripting a Wrkflow for TextEdit

Dear all,

I just discovered that TextEdit can print the filename in the header! THIS IS GREAT!
Now i discovered that I can drag a pdf File into an newly opened TextEdit File. This looks good and it also prints with the filename.

So I need help for making the following workflow:
Creating a new TextEdit File
Drag a pdf file into the Opene TextEdit File Window
Copy the Name of the PDF file into the clipboard
Close the TextEdit File
While Asked for the File Name, copy the clipboard as new Filename
Save TextEdit file…

Repeat action with all pdfs of a folder…

I really appreciate your answers and help

Best Wishes
Bernhard

Hi Bernhard,

Welcome to MacScripter!

Hopefully this is what you’re looking for.

tell application "Finder"
	set thisFolder to choose folder with prompt "Please select the folder you wish to process." without invisibles
	set theseFiles to files of thisFolder as alias list
end tell


tell application "TextEdit"
	activate

	repeat with thisFile in theseFiles

		set newFilename to my getFilename(thisFile)
		tell application "Finder" to set thePath to (container of (thisFile)) as string
		set e to make new document
		
		tell e
			make new attachment with properties {file name:thisFile}
			set pathtofile to (thePath & newFilename & ".rtfd") as Unicode text
			save in file pathtofile
		end tell
		
		close front document saving no
		
	end repeat
	
end tell


on getFilename(thisFile)
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set theFilename to last text item of (thisFile as text)
	set AppleScript's text item delimiters to "."
	set newFilename to first text item of theFilename
	set AppleScript's text item delimiters to atid
	return newFilename
end getFilename

Hi TecNik.

Nice script. I didn’t know about TextEdit’s ‘attachments’, though I see they date back at least to Leopard!

Since the container of thisFile is already known (it’s thisFolder), you could simply set thePath to thisFolder as text as soon as you’ve got thisFolder. Then you wouldn’t need to keep calling the FInder’s ‘container’ function inside the repeat and you wouldn’t have a Finder command inside the TextEdit ‘tell’ statement.

Hi Nigel,

I’d not realised you could attach things like that until I went digging around.
A nice little exercise though.

Thanks for the improvement suggestion, I’ve amended the script accordingly.

tell application "Finder"
	set thisFolder to choose folder with prompt "Please select the folder you wish to process." without invisibles
	set theseFiles to files of thisFolder as alias list
end tell


tell application "TextEdit"
	activate
	
	repeat with thisFile in theseFiles
		
		set newFilename to my getFilename(thisFile)
		
		set thisDoc to make new document
		
		tell thisDoc
			make new attachment with properties {file name:thisFile}
			set pathtofile to ((thisFolder as string) & newFilename & ".rtfd") as Unicode text
			save in file pathtofile
		end tell
		
		close front document saving no
		
	end repeat
	
end tell

on getFilename(thisFile)
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set theFilename to last text item of (thisFile as text)
	set AppleScript's text item delimiters to "."
	set newFilename to first text item of theFilename
	set AppleScript's text item delimiters to atid
	return newFilename
end getFilename

Found some useful stuff here from Nigel Garvey regarding saving TextEdit files:
http://macscripter.net/viewtopic.php?id=26401

Hello TecNic, hello Nigel

You both are great!!
thanks for the scripts, they are working, but at a point I get this error:

error “žtext item 1 of ""” kann nicht gelesen werden." number -1728 from text item 1 of “”
See picture for the filenames, script is stopping at 024…
https://www.dropbox.com/s/qh1w9pmk7tja93x/Bildschirmfoto%202014-12-09%20um%2018.48.01.png?dl=0

Till then the script works perfect. Thanks a lot for now :slight_smile:

Best Wishes
Bernhard

Hi.

The problem could be in the getFilename() handler, which assumes that the file’s name comes after the last colon in the path. But if the ‘file’ is actually a bundle, its path will end with a colon, so the handler will think its name is “”.

If that’s the problem here, this version of the handler may work better:

on getFilename(thisFile)
	set thisFile to thisFile as text
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	if (thisFile ends with ":") then
		set theFilename to text item -2 of thisFile
	else
		set theFilename to last text item of thisFile
	end if
	if (theFilename contains ".") then
		set AppleScript's text item delimiters to "."
		set theFilename to text 1 thru text item -2 of theFilename
	end if
	set AppleScript's text item delimiters to atid
	return theFilename
end getFilename

Edits: Corrected in the light of Yvan’s and Bernhard’s comments below. And TecNik’s!

Hello Nigel

Am I misunderstanding ?
The handler splits the name in two parts supposed to be :
the stripped name
the name extension

From time to time, I receive files whose name contains other periods than the one before the name extension.
In this case the handler will not return what is wanted.

I would not use
set newFilename to first text item of theFilename
but
set newFileName to (text items 1 thru -2 of theFileName) as text

Yvan KOENIG (VALLAURIS, France) mardi 9 décembre 2014 19:56:14

Hi Yvan.

Yes. That’s even better. I’ll edit my post above.

Now the script stops and says:

the variable “new filename” is not defined…

Hello Nigel and Teknic,

Can you script it that it only uses the pdf`s also?

Thanks a lot!
Best Wishes
Bernhard

Ah. My mistake. Now corrected (post #6).

The variable newFilename was only being set when the name of the file contained “.”. That must mean your files (or some of them) don’t have name extensions. (I see from your screen shot that this is indeed so. Even one of your PDFs is extensionless.)

Hi All,

When I try that new handler in the script I get 1 document named as the enclosing folder?

As for my original script, I work in a pdf environment and there was an assumption I was working with a folder full of pdfs with a .pdf extension.

Ach! I incompetently transposed the ‘if’ and ‘else’ actions. Now corrected. Thanks, TecNik. Sorry, Bernhard.

In a folder with a mixture of files, such as Bernhard apparently has, .pdf name extensions would make the identification of PDFs easy and fast. But Bernhard’s screen shot shows a folder listing in which some names have extensions and others don’t. This is even true with his PDFs (as identified in the just-visible “Kind” column). The fact that there’s a mixture suggests (to me) that the missing extensions really are missing and not just hidden according to his Finder preferences. On the other hand, if I remove the extension from a PDF on my own computer, the file’s no longer recognised as a PDF Document in the “Kind” column. I’m too puzzled to continue at the moment. :confused:

Some applications have a setting allowing them to hide the extension even if the Finder is supposed to display it.
Maybe it’s the case for the one creating the PDFs with no visible extension.
If my memory is right, iWork '09 applications offered the described feature for their documents.

Yvan KOENIG (VALLAURIS, France) jeudi 11 décembre 2014 15:48:01

All PDF´s were converted correctly!
Great Guys!!
Thanks a lot.

As a newbie, may I ask one ore question?

I want to tell Text edit to open one ore some or all Documents, open the print dialogue for each doc and print it.

Any suggestions?

Thanks in Forward.
Bernhard

Here is a piece of code which I use to print the current TextEdit document in a PDF file.


# Saves the current TextEdit document as PDF using the Print dialog "Save as PDF" option.

set maxloops to 100

set process_name to "TextEdit"
if (do shell script "defaults read 'Apple Global Domain' AppleLocale") does not start with "fr_" then
	set Print_loc to "Print"
	set SaveAsPDF_loc to "Save as PDF."
	set Save_loc to "Save"
else
	set Print_loc to "Imprimer"
	set SaveAsPDF_loc to "Enregistrer au format PDF."
	set Save_loc to "Enregistrer"
end if

tell application "System Events" to tell process process_name
	set frontmost to true
	# Open the print dialog
	keystroke "p" using command down
	
	# Wait until the Print dialog opens before proceeding
	
	set prSheetRef to 0
	repeat maxloops times
		if exists sheet 1 of window 1 then
			set prSheetRef to a reference to sheet 1 of window 1
			exit repeat
		else
			delay 0.1
		end if
	end repeat
	if prSheetRef = 0 then
		display dialog "No Print sheet available."
		return
	end if
	# Expand the "PDF" menu button before triggering a menu item.
	click menu button "PDF" of prSheetRef
	
	# Wait until the menu button menu is available.
	set ready to false
	repeat maxloops times
		set ready to exists menu item SaveAsPDF_loc of menu 1 of menu button "PDF" of prSheetRef
		if ready then exit repeat
		delay 0.1
	end repeat
	if not ready then
		display dialog "No Save as PDF dropdown menu available."
		return
	end if
	# Select the "Save as PDF" menu item
	click menu item SaveAsPDF_loc of menu 1 of menu button "PDF" of prSheetRef
	
	
	# Wait until the Save sheet opens before proceeding
	set ready to false
	repeat maxloops times
		set ready to exists sheet 1 of sheet 1 of window 1
		if ready then exit repeat
		delay 0.1
	end repeat
	if not ready then
		display dialog "No Save sheet available."
		return
	end if
	# Use belt and suspenders. Check that we really get the wanted sheet.
	set ready to false
	repeat maxloops times
		set ready to (name of buttons of sheet 1 of sheet 1 of window 1) contains Save_loc
		if ready then exit repeat
		delay 0.1
	end repeat
	if not ready then
		display dialog "No Save sheet available."
		return
	end if
	# Force Desktop as location to save in
	keystroke "d" using {command down}
	# Now define the file name
	--
	# validate the save request
	keystroke return
	
end tell # System Events

You may use it a a starting point to achieve your goal.

Yvan KOENIG (VALLAURIS, France) samedi 13 décembre 2014 16:56:26