Page 1 of 1

Possible to display image in prompt?

PostPosted: Fri Dec 07, 2012 10:47 pm
by mvm
Hey all,

I have been using hazel for a couple years now with very few problems. Heres my problem / question:

I frequently download pdf files that have a seemingly random naming system (sometimes abbreviations, sometimes random assortment of letters and numbers, etc) as they are for the classes I am taking and come from multiple sources. What I am trying to do is have Hazel search my downloads folder for pdfs and when one is found, bring up a prompt with about 6 different possible folders (i.e. not a location browser, preferably a set of radio buttons or drop down list), as well as the name of the file and a quick look / preview of the pdf so I can see at least the first page and determine the appropriate location to send it.

Using Pashua, I have managed to bring up a prompt with the first page of the pdf and folder locations, but I cannot for the life of me figure out how to modify a actionscript with pashua to work with hazel.....

Is this within hazels capabilities?

Re: Possible to display image in prompt?

PostPosted: Mon Dec 10, 2012 3:29 pm
by Mr_Noodle
I don't have any experience of Pashua but it looks like it supports AppleScript. In that case, consult the help for AppleScript as it will tell you how to structure the AppleScript and receive arguments and return values back.

Re: Possible to display image in prompt?

PostPosted: Tue Dec 11, 2012 1:17 pm
by a_freyer
TL;DR - search forums for quicklook, custom matching, and subfolder sorting.

I have a rule setup that does nearly exactly what you're looking for, but more future-proof in terms of expansion to more than five sorting styles.

First to note is that all my files are named with a prefix of the folder they appear in:

/Archive/Personal/Personal - W2 2011.pdf
/Archive/Personal/Personal - W2 2012.pdf

/Archive/Receipts/Receipts - Bank Transfer 11-2-12.pdf
/Archive/Receipts/Receipts - Living Room Couch.pdf

/Archive/Tickets/Tickets - Concert at Bluebird.pdf
/Archive/Tickets/Tickets - Southwest Thanksgiving Flight.pdf


This is a wonderful way to sort things with Hazel. Once you master custom matching (view my tutorial here), you can simply rename your files and Hazel sorts them properly from there. For this reply, every time I use a • character, I am using a custom token.

But here's the first problem: Not all my pdfs go into a subfolder of Archive. Like you, I have thousands of different locations for PDFs that are not within my archive...

RULE #1: Is the file named with a tag & does that tag exist in ARCHIVE?
Code: Select all
if (all) of the following conditions are met for (the file or folder being matched)
    Name matches (search token •) - (•FilenameWithoutToken)
    Passes AppleScript (Embedded Script)

Do the following to the matched file or folder:
    Display notification
    Move to folder Archive
    Sort into subfolder with pattern (search token •)


This rule tests first if there is a (TOKEN) space dash space (OTHER TEXT), and then second if that token exists as a folder in the Archive.

To test if the folder exists in the archive, I run the following script. This returns FALSE if the token is not in the Archive folder as a folder and TRUE if it is:

Code: Select all
#first isolate the file name
tell application "Finder" to set theFileNameFull to (name of theFile)

#then we separate the file name from its search token
set AppleScript's text item delimiters to " - "
set theSplitFileNameList to theFileNameFull's text items
set AppleScript's text item delimiters to {""} --> restore delimiters to default value

#full separation of the file name into sort token and
set theSortToken to item 1 of theSplitFileNameList
set theFileNameWithoutSortToken to item 2 of theSplitFileNameList

#see if that folder exists anywhere
set thecmd to "mdfind -onlyin '/Users/andrewfreyer/Dropbox/Archive/' \"kMDItemContentType==public.folder && kMDItemFSName=='" & theSortToken & "\""

try
   set theResult to do shell script thecmd
   
   if (theResult is equal to "") then
      return false
   else
      return true
   end if
on error
   return false
end try

return false



Alright, well this is fine and dandy. If a file appears in my watched directory that has a certain name format, it can be intelligently moved to an Archive folder.

Not particularly helpful for your situation... just yet. The problem is that we just downloaded the file and we don't want to navigate to it in Finder just to rename it. Enter rule #2:

RULE #2: Preview the file & ask if Hazel should create a new archive directory, or which archive directory you'd like this file sorted into?


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

Do the following to the matched file or folder;
    Run shell script (embedded)
    Run applescript (embedded [with output tokens])
    Rename with (SortToken •)(name)(extension)


The embedded shell script passes the file to quicklook, so that you can see what it is:

Code: Select all
qlmanage -p "$1" >& /dev/null &


The embedded applescript asks you what prefix you would like to append to this file (make sure you add the SortToken in the [i] listing:

Code: Select all
#stupid delay because qlmanage doesn't show consistently
delay 1

#don't forget that theFile is the variable for the file
set theDialogText to "What is the appropriate prefix for this file?

" & theFile
set theButtonIfPressedTheRuleWillContinue to "Rename & Sort"
set theButtonIfPressedRuleAborts to "Cancel"
set theDialogTitle to "Advanced Hazel Sorting"

############## This is where the magic happens ############
tell application "System Events"
   activate
   set theReply to (display dialog theDialogText with title theDialogTitle buttons {theButtonIfPressedTheRuleWillContinue, theButtonIfPressedRuleAborts} default button 1 giving up after 60 default answer "" 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

set theButtonResult to button returned of theReply
set theSortToken to text returned of theReply & " - "

if (theSortToken is equal to "") or (theButtonResult is theButtonIfPressedRuleAborts) then set theButtonResult to ""

return {hazelExportTokens:{SortToken:theSortToken}}


And there you have it!