Change Filename with Clipboard-Text

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

Moderator: Mr_Noodle

Change Filename with Clipboard-Text Fri Jan 21, 2011 5:29 am • by K.Meier
Hi,

I automatically download my daily newspaper as PDF and store it in a Folder. The PDFs have the naming-scheme:
"2011.01.21 - Newspapername.PDF"

When I find an article in the newspaper I want to keep, I just drag an drop that page from the sidebar of Preview.app to my Desktop, where this page then has the naming-scheme:
"2011.01.21 - Newspapername (moved).PDF"

Since this is not the best way for finding interesting articles, I manually rename this file. I copy (cmd + c) the headline of the article in Preview.app and rename the file on my desktop to
"2011.01.21 - Headline.PDF"

Then I move the file to a folder where all my interesting articles are stored.

Since I am a long-time-hazel-user I really would like Hazel to do the following:
1. I copy (cmd + c) the headline of the article
1a. Hazel ideally reformats the rtf-Text in the clipboard to plain Text!
2. I drag the page to my desktop
3. Hazel renames the file ("2011.01.21 - Newspapername (moved).PDF") to "2011.01.21 - Headline.PDF" (with the headline i just copied to Clipboard)
4. Hazel moves the file to the folder I use to store my interesting articles. (that's the easy part even I could handle! ;-) )

Alternatively Hazel could ask me for the headline with some script and I paste the Headline and then Hazel renames the (moved) file.

Do you think that can be done? Does anybody have any hints for that?

Thanks very much for your hints/thoughts!

K. Meier
K.Meier
 
Posts: 4
Joined: Fri Jan 21, 2011 5:12 am

Re: Change Filename with Clipboard-Text Fri Jan 21, 2011 3:20 pm • by dhy8386
You will need to use applescript.

But what do you do if you find 4 interesting articles. Arent they all the same name? Or is each page a different story and therefore as you click from page to page in your PDF, the header window name changes? I dont fully understand.

I ask because an applescript which Hazel triggers anytime a file with the name (moved) in it hits the desktop is not hard, assuming the header is always the same for each newspaper your reading.

To your idea about a popup box asking you to rename the file, that i believe is also not difficult but you are better off with the above because its less interaction by you.
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

Re: Change Filename with Clipboard-Text Fri Jan 21, 2011 7:55 pm • by dhy8386
Sorry thats not very helpful. I dont know how to do the first part but if you are ok with have a pop where you type in the rename, here is what I would do.

Hazel Rule 1 on the Desktop
If all
Name of file contains (moved)
Date added is after Date Last Matched
then
Run embedded applescript [and here is the code to use]
Code: Select all
tell application "Finder" to activate
tell application "Finder"
   set currentName to name of theFile
   set name of theFile to text returned of (display dialog "Re-name File:" default answer currentName)
end tell

tell application "Finder"
   set label index of theFile to 2
end tell


This will pop a window asking you to rename every file found (moved) in the name. The default name in the box is the name of your file so you will be able tell which files you are renaming. Once you rename the file, it turns it red.

Hazel rule 2 on the desktop
If all
Color label of file is Red
Then
Set color label to blank (the "x")
Move file to your folder of choice

This way most of it is automated. You just need to manually rename as you drag different articles to the desktop.
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

Re: Change Filename with Clipboard-Text Sat Jan 22, 2011 4:43 pm • by K.Meier
Dear dhy8366,

you saved my evening! Having never really understood how Applescript works, you provided me with a great tutorial with your script. Thanks to you I could
1. Manage to get what I needed
2. Get into Applescript-scripting
3. Managed to change your Applescript to get the Clipboard-Text to the filename.

Here is my Applescript (which is started after the Hazel-Rules you provided me):

Code: Select all
tell application "Finder" to activate
tell application "Finder"
   set dateName to name of theFile
   set thePrefix to text 1 thru ((offset of "." in dateName) - 1) of dateName
   set the clipboard to «class ktxt» of ((the clipboard as text) as record)
   set theCaption to the clipboard
   set theYear to text 1 thru 4 of thePrefix
   set theMonth to text 5 thru 6 of thePrefix
   set theDay to text 7 thru 8 of thePrefix
   set theDate to theYear & "." & theMonth & "." & theDay & " - "
   set name of theFile to theDate & theCaption
   set name extension of theFile to "pdf"
end tell


After this I have a Hazel-Rule to move the file to the desired folder.

In my Applescript I do the Year-Month- and Day-Thing to get a period (".") between the Year, Month and Day which I didn't mention in my original post to not ask too many questions here. The original name of the downloaded file is "20110121_Newspapername.PDF

I was able to change your script with help from the following websites:
http://hints.macworld.com/article.php?s ... 4170653788 (remove text-styles)
http://hintsforums.macworld.com/showthread.php?t=84244 (add extension "pdf" to the name)
http://www.mactech.com/articles/mactech ... index.html (make the date-stuff work)
http://www.devdaily.com/blog/post/mac-o ... e-comments (add comments, which I will do)

The only thing I couldn't get working was the problem of carriage-returns in headlines I copy from the pdf. But I will try to get that done as well!

Thank you so much!

K.Meier
K.Meier
 
Posts: 4
Joined: Fri Jan 21, 2011 5:12 am

Re: Change Filename with Clipboard-Text Sat Jan 22, 2011 8:01 pm • by dhy8386
Glad i could help. Good to see you get into applescript. The best site for finding answers in case you have not come across it is macscripter.net.

See this post here about carriage return removal. I cant figure out from your script exactly where it should go but i am sure you can figure it out.

http://macscripter.net/viewtopic.php?id=18203
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

Re: Change Filename with Clipboard-Text Mon Jan 24, 2011 7:09 pm • by K.Meier
Ok, I finally did it!

Here is the code (above mentioned Hazel-Rules have not changed):

Code: Select all
tell application "Finder" to activate
tell application "Finder"
   set dateName to name of theFile
   set thePrefix to text 1 thru ((offset of "_" in dateName) - 1) of dateName # take the first part of the filename until _
   set the clipboard to «class ktxt» of ((the clipboard as text) as record) # remove styles from the clipboard
   set theCaption to the clipboard
   set AppleScript's text item delimiters to {(ASCII character 13) as text} # tell Applescript to look for an item, here: carriage return
   set mylines to text items of theCaption # make a temporary copy of the text items of the clipboard
   set AppleScript's text item delimiters to {" - "} # tell Applescript to replace the found item with " - "
   set theCaption to mylines as text # restore the clipboard with the modified clipboard
   set AppleScript's text item delimiters to {":"} # tell Applescript to look for an item, here: ":"
   set mylines to text items of theCaption # make a temporary copy of the text items of the clipboard
   set AppleScript's text item delimiters to {" - "} # tell Applescript to replace the found item with " - "
   set theCaption to mylines as text # restore the clipboard with the modified clipboard
   set AppleScript's text item delimiters to {""} # restore Applescript's item delimiters to the original ones
   set theYear to text 1 thru 4 of thePrefix
   set theMonth to text 5 thru 6 of thePrefix
   set theDay to text 7 thru 8 of thePrefix
   set theDate to theYear & "." & theMonth & "." & theDay & " - "
      copy length of theCaption to theCaptionlength # test the length of theCaption
      if theCaptionlength > 230 then
      set theCaption to text 1 thru 230 of theCaption # respect the maximum filename-length of 255. Some characters obviously don't count so the limit is set low here
      else
      set theCaption to theCaption # do nothing
      end if
   set name of theFile to theDate & theCaption
   set name extension of theFile to "pdf"
end tell


Thank you again! :-)

And one more thank you to:
http://www.macwindows.com/convertr.html (maximize file-length)
http://discussions.apple.com/thread.jsp ... tstart=225 (remove ":" and carriage return)

K. Meier
K.Meier
 
Posts: 4
Joined: Fri Jan 21, 2011 5:12 am


Return to Support