automatically adding a new text file (RTF)

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

automatically adding a new text file (RTF) Tue May 28, 2013 4:03 pm • by haricot
Hello,

I'm a new user and am trying to get Hazel and Automator to work together to do the following:

1. When a new PDF file is dropped on a folder,
2. Open it with Preview,
3. Ask me to rename it,
4. Rename the file with the name I specify,
5. Move the newly renamed file to a folder with the same name, and
6. Add an RTF file to the new folder with the same name as the PDF file plus the word "Notes" at the end.

I'm using it to sort academic articles. So here's how I want it to work in practice [and what's happening in brackets]:

1. I add GARBAGENAMEPDFWITHLOTSOFMEANINGLESSNUMBERS to my "Article File" folder. [ok, this works.]
2. Hazel tells Automator to open it with Preview and ask me for a new name. [no problem here.]
3. Automator/Applescript asks me to rename the file with the Author Date syntax I specify from looking at the file (sadly, academic presses have not heard of PDF metadata.) [only problem here is that the window Automator opens doesn't receive the focus, so I have to use the mouse to activate it before I can type in the new name and file]
4. Automator changes the file name [works fine]
5. Automator moves the file to a new folder [but since I'm using the "New Folder" command, it also leaves a copy in the "Article File" folder, which triggers a loop in Hazel.]
6. Add an RTF file to the new folder with the same name as the PDF file [this works fine one way, but see below.]

One thing that would help is to split this up into discrete actions so that automator isn't trying to do everything at once and I don't end up with two copies of the file in step 5, which is what is triggering the loop. But to do that requires changing the Automator workflow so that it can accept whatever information Hazel is passing to it when the "sort into subfolder" action is selected. Is it passing the FILE itself to Automator? The POSIX address of the file? The newly created subfolder? I am stumped.

Thanks.

J
haricot
 
Posts: 2
Joined: Tue May 28, 2013 3:48 pm

Woo hoo! I got it to work. One issue was that I had gotten confused by the error handling in the try statement. There were probably others.

In a day of heavy googling, I haven't found a way to make this work, so I'm posting it here. If there's a better way perhaps it can end up in the tips to help the next person like me.

Code: Select all
--portions of the below adapted from a post by UniAce at MacOSXHints. See http://hintsforums.macworld.com/member.php?u=33716
--set hazelStop to false
set theFile to choose file
set _fname to name of (info for theFile)
set the sourceFolder to POSIX file (do shell script "dirname " & quoted form of POSIX path of theFile) as alias

--this next part removes ".pdf" from the end of the filename, if it happens to be there
if _fname contains "." then
   set old_delimiters to AppleScript's text item delimiters
   set AppleScript's text item delimiters to "."
   set file_ext to (last text item of _fname) as text
   set num_textitems to (count of text items of _fname)
   set _fname1 to (text items 1 thru (num_textitems - 1) of _fname) as text
   set AppleScript's text item delimiters to old_delimiters
   if file_ext = "pdf" then
      set _fname to _fname1
   end if
end if

--if a note file with this name already exists in this place, return to Hazel with hazelStop
set fileTarget to (sourceFolder as text) & _fname & ".rtf"

--test to see if there's already a file called Filename Notes.rtf. If there isn't, the script will consider it an error, and go on to create the new file. If there is one, it will stop the script and set the hazelStop variable to true, so that Hazel should know to stop working on the file.
try
   fileTarget as alias
   set hazelStop to true
   return
   
on error
   set sourceFolderPath to (POSIX path of sourceFolder) as string --converts colons to slashes
   set NewFilePath to (sourceFolderPath & _fname & ".rtf") --add the filename and .rtf file extension to the path
   --the below contents are the minimal contents needed for TextEdit to open the new rtf file, for some reason.
   set NewRTFcontents to "{}"
   
   do shell script ("echo  '" & NewRTFcontents & "' > " & quoted form of NewFilePath) --makes the new file. using "quoted form" takes care of anything weird in the path or file name, like spaces or punctuation.
   
end try






J
haricot
 
Posts: 2
Joined: Tue May 28, 2013 3:48 pm

Re: automatically adding a new text file (RTF) Wed May 29, 2013 10:24 am • by a_freyer
This is a bit more simple (does not require Preview to be open, does not attempt to parse filenames, does not require you to choose the file first, does not require automator):

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     kind is PDF

Do the following to the matched file or folder:
     run AppleScript (embedded script)
     move to /parent folder/of/new/folders/
     sort into subfolder with pattern (name)
     run shell script (embedded script)


Applescript:
Code: Select all
###################### Here is where we set the dialog text #################
set theDialogText to "Type a new name for this file:"
set theButtonIfPressedTheRuleWillContinue to "Rename"
set theButtonIfPressedRuleAborts to "No Change"
set theDialogTitle to "Rename PDF File"

################### Name of the File ###########################

tell application "Finder" to set theName to name of theFile

############## This is where the magic happens ############
tell application "System Events"
   activate
   set theResult to (display dialog theDialogText with title theDialogTitle buttons {theButtonIfPressedTheRuleWillContinue, theButtonIfPressedRuleAborts} default answer theName default button 1 giving up after 60 with icon (POSIX file (POSIX path of (get path to library folder from user domain) & "/PreferencePanes/Hazel.prefPane/Contents/Resources/PrefPane.icns" as string) as string) as alias)
end tell

if (button returned of theResult is theButtonIfPressedTheRuleWillContinue) then
   set theName to text returned of theResult
end if

tell application "Finder" to set the name of theFile to theName


Shell Script:
Code: Select all
filename=$(basename "$1")
filename="${filename%.*}"
echo '{\rtf1}' > $(dirname "$1")/$filename-Notes.rtf
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado


Return to Support

cron