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…
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
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.
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
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!
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
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.)
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.
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.
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.