Prompt to Rename Files

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

Moderator: Mr_Noodle

Prompt to Rename Files Wed Apr 04, 2012 11:12 pm • by murr
I am having a difficult time coming up with a solution and was hoping for some assistance. I am running Hazel 3.0 on Lion.

Background: I take a lot of screen shots and they all go to the desktop. Sometimes I take 20-30 screen shots all within a couple of minutes.

What I am looking to do is have Hazel monitor my Desktop for files that begin with name Screen Shot and once one is detected prompt me to rename it. I know how to setup up the monitoring rule but not the prompt rule. So basically, I would have the opportunity to rename a screen shot every time one is created.

Is this possible to do with Hazel? I have no experience with Automator or Applescript.

Thanks for any help you can offer.
murr
 
Posts: 8
Joined: Wed Apr 04, 2012 9:23 pm

Re: Prompt to Rename Files Thu Apr 05, 2012 12:41 pm • by Mr_Noodle
You'd need to do an AppleScript and export a token back to Hazel to do the renaming. Check the help on AppleScript as there are more details there.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Prompt to Rename Files Thu Apr 05, 2012 5:09 pm • by sjk
I've noticed screen capture apps on MacUpdate, possibly some free, that support interactive renaming after the capture. One of those might be a viable alternative if you get stuck doing it with Hazel + scripting.
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

Re: Prompt to Rename Files Thu Apr 05, 2012 7:00 pm • by a_freyer
Try this:

Code: Select all
if (all) of the following...
    Name MATCHES Screen Shot (...)

then
    Run AppleScript (exporting NewFilename)
    Move, or whatever else.


And the applescript:

Code: Select all
tell application "Finder" to set shortName to name of (theFile as alias)

set theDialogText to "Type the new name of the Screen Shot: " & shortName
set theButtonIfPressedTheRuleWillContinue to "Rename Screen Shot"
set theButtonIfPressedRuleAborts to "Skip File"
set theDialogTitle to "Advanced Hazel Renaming"

tell application "System Events"
   activate
   set theResult to (display dialog theDialogText with title theDialogTitle default answer "" buttons {theButtonIfPressedTheRuleWillContinue, theButtonIfPressedRuleAborts} 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/Hazel.icns" as string) as string) as alias)
end tell

if (button returned of theResult) is theButtonIfPressedTheRuleWillContinue then
   set theText to text returned of theResult
   return {hazelExportTokens:{NewFilename:theText}}
end if

return {hazelExportTokens:{NewFilename:shortName}}
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Prompt to Rename Files Thu Apr 05, 2012 7:31 pm • by murr
Thank you all for your replies.

a_freyer: I created the applescript file and setup the Hazel rule as you suggested and ran it. It doesn't complete properly. I am getting errors and I don't know what these mean. Can you assist me with them? Here is the log info:

2012-04-05 19:25:49.832 (null)[0] Screen Shot 2012-04-04 at 3.38.02 PM.png: Rule Screen Shot Rename matched.
2012-04-05 19:25:49.975 hazelworker[43898] [Error] AppleScript failed: Error executing AppleScript on file /Users/murr/Desktop/Screen Shot 2012-04-04 at 3.38.02 PM.png.
2012-04-05 19:25:49.976 hazelworker[43898] AppleScript error: {
NSLocalizedDescription = "Can\U2019t make \U00abclass psxf\U00bb \"/Users/murr/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\" of application \"System Events\" into type string.";
NSLocalizedFailureReason = "Can\U2019t make \U00abclass psxf\U00bb \"/Users/murr/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\" of application \"System Events\" into type string.";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: 'null'()>";
OSAScriptErrorBriefMessageKey = "Can\U2019t make \U00abclass psxf\U00bb \"/Users/murr/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\" of application \"System Events\" into type string.";
OSAScriptErrorExpectedTypeKey = "<NSAppleEventDescriptor: 'TEXT'>";
OSAScriptErrorMessageKey = "Can\U2019t make \U00abclass psxf\U00bb \"/Users/murr/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\" of application \"System Events\" into type string.";
OSAScriptErrorNumberKey = "-1700";
OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'furl'(\"file://localhost/Users/murr/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\")>";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
2012-04-05 19:25:49.979 hazelworker[43898] Sending metrics to scheduler. Next scheduled run: 5828963-12-19 19:00:00.000
murr
 
Posts: 8
Joined: Wed Apr 04, 2012 9:23 pm

Re: Prompt to Rename Files Thu Apr 05, 2012 7:38 pm • by a_freyer
It looks like you have hazel installed for all users instead of just yourself. The way you should modify the code is this:

EDIT - corrected.

Code: Select all
tell application "Finder" to set shortName to name of (theFile as alias)

set theDialogText to "Type the new name of the Screen Shot: " & shortName
set theButtonIfPressedTheRuleWillContinue to "Rename Screen Shot"
set theButtonIfPressedRuleAborts to "Skip File"
set theDialogTitle to "Advanced Hazel Renaming"

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

if (button returned of theResult) is theButtonIfPressedTheRuleWillContinue then
   set theText to text returned of theResult
   return {hazelExportTokens:{NewFilename:theText}}
end if

return {hazelExportTokens:{NewFilename:shortName}}
Last edited by a_freyer on Fri Apr 06, 2012 12:47 pm, edited 1 time in total.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Prompt to Rename Files Thu Apr 05, 2012 7:39 pm • by a_freyer
This is what you'll see:

Image
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Prompt to Rename Files Thu Apr 05, 2012 8:44 pm • by a_freyer
I also forgot to say that you'll want to include an ignore flag.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Prompt to Rename Files Thu Apr 05, 2012 11:28 pm • by murr
For the record, the prompt you have in the screen shot is exactly what I am looking for. I changed the embedded script you provided in your latest post and restarted Hazel. This is the error I am getting now in the log file.

2012-04-05 23:24:31.215 (null)[0] Screen Shot 2012-04-04 at 3.38.02 PM.png: Rule Screen Shot Rename matched.
2012-04-05 23:24:31.344 hazelworker[71531] [Error] AppleScript failed: Error executing AppleScript on file /Users/murr/Desktop/Screen Shot 2012-04-04 at 3.38.02 PM.png.
2012-04-05 23:24:31.346 hazelworker[71531] AppleScript error: {
NSLocalizedDescription = "Can\U2019t make \U00abclass posx\U00bb of \"/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\" into type string.";
NSLocalizedFailureReason = "Can\U2019t make \U00abclass posx\U00bb of \"/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\" into type string.";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: 'null'()>";
OSAScriptErrorBriefMessageKey = "Can\U2019t make \U00abclass posx\U00bb of \"/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\" into type string.";
OSAScriptErrorExpectedTypeKey = "<NSAppleEventDescriptor: 'TEXT'>";
OSAScriptErrorMessageKey = "Can\U2019t make \U00abclass posx\U00bb of \"/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\" into type string.";
OSAScriptErrorNumberKey = "-1700";
OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'obj '{ 'form':'prop', 'want':'prop', 'seld':'posx', 'from':'utxt'(\"/Library/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns\") }>";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
murr
 
Posts: 8
Joined: Wed Apr 04, 2012 9:23 pm

Re: Prompt to Rename Files Thu Apr 05, 2012 11:38 pm • by a_freyer
The location of your pref pane and place that in the text of the script.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Prompt to Rename Files Fri Apr 06, 2012 10:54 am • by murr
i'm a little confused. the pref pane location is set correctly in the script (i believe). maybe i am not understanding this correctly. attached is a pic of the location.

Image
murr
 
Posts: 8
Joined: Wed Apr 04, 2012 9:23 pm

Re: Prompt to Rename Files Fri Apr 06, 2012 12:46 pm • by a_freyer
My apologies, I've been debugging this without a computer and now I see the problem:

Change the icon code to this:

Code: Select all
with icon (POSIX file (POSIX path of (get path to library folder) & "/PreferencePanes/Hazel.prefPane/Contents/Resources/Hazel.icns" as string) as string) as alias)
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Prompt to Rename Files Fri Apr 06, 2012 1:03 pm • by murr
ok.. we are making some headway now. i get the prompt but when i assign the new name nothing happens. here is the log file:

2012-04-06 13:01:14.411 (null)[0] Screen Shot 2012-04-04 at 3.38.02 PM.png: Rule Screen Shot Rename matched.
2012-04-06 13:01:26.396 hazelworker[56813] Sending metrics to scheduler. Next scheduled run: 2012-04-11 15:41:06.000
2012-04-06 13:01:45.606 hazelworker[56793] AppleScript returns value for token hazelcustomtoken:NewFilename but no such token was declared in the interface.
2012-04-06 13:01:45.613 hazelworker[56793] Sending metrics to scheduler. Next scheduled run: 5828963-12-19 19:00:00.000
murr
 
Posts: 8
Joined: Wed Apr 04, 2012 9:23 pm

Re: Prompt to Rename Files Fri Apr 06, 2012 1:06 pm • by a_freyer
You have to add the token manually - click the (i) in the upper right hand corner of the embedded script window and you'll see the place where you need to type "NewFilename"
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Prompt to Rename Files Fri Apr 06, 2012 1:27 pm • by murr
ok.. i'm not sure if this is correct. i added the token here:

Image

when i run the rule now, the pop up shows, i rename the file, click the button and nothing happens. the filename is the same as it was. the log file doesn't have any info to assist either.

Image
murr
 
Posts: 8
Joined: Wed Apr 04, 2012 9:23 pm

Next

Return to Support

cron