[Question] Tested Applescript works not with Hazel

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

Moderator: Mr_Noodle

Hi,

i searched the Forum for a solution of my following problem and asked Google. So here i am to ask you for help, because i have no more idea.

I scan my receipts, OCR it and extract the date with Hazel's fantastic Date Matching feature. The matched Date (var = receiptDate) will then be used to rename the files with following syntax

Code: Select all
[yyyy-mm-dd] receipt - Something i bought.pdf


Finally i want to set the receiptDate as the Creation Date of the File and created following Applescript, which works perfectly in the Applescript editor.

Code: Select all
---property theFile : alias "Path:To:Your:Example File.extension"
set theFile to choose file

tell application "Finder"
   
   --- Get Name of theFile
   set theFileName to (text items 1 thru ((length of (name of theFile as string)) - 4) of (name of theFile as string)) as string
   
   --- Convert Posix Path of theFile to a quoted one
   set theQuotedFilePath to quoted form of (POSIX path of (theFile)) as string
   
   --- Extract Date from Filename
   set yyyy to text 2 through 5 of theFileName
   set mm to text 7 through 8 of theFileName
   set dd to text 10 through 11 of theFileName
   
   --- Get actual Date and Time
   set currentDate to quoted form of (do shell script "date '+%m/%d/%Y %H:%M':%S")
   
   --- Extract Actual Time from currentDate
   set h to text 13 through 14 of currentDate
   set m to text 16 through 17 of currentDate
   set s to text 19 through 20 of currentDate
   
   --- Define new File-Creation-Date
   set creationDate to "'" & mm & "/" & dd & "/" & yyyy & " " & h & ":" & m & ":" & s & "'"
   
   ---Change Creation-Date of theFile
   do shell script "SetFile -d " & creationDate & " " & theQuotedFilePath
   
   ---Change Modification-Date of theFile
   do shell script "SetFile -m " & currentDate & " " & theQuotedFilePath
   
end tell


So i embedded this Applescript into a very simple Hazel rule, which only searches for a pdf and executes the script...

Code: Select all
tell application "Finder"

--- Get Name of theFile
   set theFileName to (text items 1 thru ((length of (name of theFile as string)) - 4) of (name of theFile as string)) as string
   
   --- Convert Posix Path of theFile to a quoted one
   set theQuotedFilePath to quoted form of (POSIX path of (theFile)) as string
   
   --- Extract Date from Filename
   set yyyy to text 2 through 5 of theFileName
   set mm to text 7 through 8 of theFileName
   set dd to text 10 through 11 of theFileName
   
   --- Get actual Date and Time
   set currentDate to quoted form of (do shell script "date '+%m/%d/%Y %H:%M':%S")
   
   --- Extract Actual Time from currentDate
   set h to text 13 through 14 of currentDate
   set m to text 16 through 17 of currentDate
   set s to text 19 through 20 of currentDate
   
   --- Define new File-Creation-Date
   set creationDate to "'" & mm & "/" & dd & "/" & yyyy & " " & h & ":" & m & ":" & s & "'"
   
   ---Change Creation-Date of theFile
   do shell script "SetFile -d " & creationDate & " " & theQuotedFilePath
   
   ---Change Modification-Date of theFile
   do shell script "SetFile -m " & currentDate & " " & theQuotedFilePath

end tell


But it doesn't work and i don't know why. In the Script Editor it will in Hazel not!? Can anyone please give me an advice how to get this script working or how i can use the var receiptDate to change the creation date of theFile!?

Thanx in advance.

Thomas

P.S. BTW - I use the latest version of Hazel on a MBP with the latest OS X Yosemite.
t_klein
 
Posts: 20
Joined: Mon Feb 27, 2012 3:50 am

Hi again,

found a nice solution for my problem on the following site of John Clayton.

http://paperless-john.postach.io/hazel-shared-custom-attributes

And this is exactly what was looking for and it works as it should. Perfect. So here is the snippet.

Code: Select all
tell application "Finder"
set the_date to item 1 of inputAttributes

set y to text -4 thru -1 of ("0000" & (year of the_date))
set m to text -2 thru -1 of ("00" & ((month of the_date) as integer))
set d to text -2 thru -1 of ("00" & (day of the_date))

set setfile_date_str to m & "/" & d & "/" & y

set cmd to "/usr/bin/SetFile -d " & "\"" & setfile_date_str & "\" \"" & (POSIX path of theFile) & "\""

do shell script cmd
end tell


But let me ask you a last question. In my rule i use the script above with a input attribute to change the file creation date and right after this rule another one, that copies a PDF right in the DEVONthink Inbox. Here's the script.

Code: Select all
tell application id "com.devon-technologies.thinkpro2"
   launch
   set theDatabase to open database "~/Documents/DEVONthink/Thomas.dtBase2"
   set theGroup to create location "Inbox" in theDatabase
   import theFile to theGroup
end tell


Each one alone works fine, but together the rule quits right after the first script (the one above) was successfully executed.

Can anyone please help me?

Thanx again.

Thomas
t_klein
 
Posts: 20
Joined: Mon Feb 27, 2012 3:50 am

Hazel only executes the first rule to match, by default. If you want a file to match multiple rules, you need to use the "Continue" action on any previous rules.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

@mr_Noodle

First, thank you for your quick reply!

Sorry, but i think i explained it wrong. I have one rule with some filters to grab the right file and the two described applescripts. Both in one rule! Here's a quick example of one rule...

Code: Select all
- get file of kind PDF

- rename the file
- run embedded applescript one (with inputAttribute)
- run embedded applescript two
- set color flag to green


If i run this rule it breaks right after applescript one!

But if i change the rule and remove applescript one or two everything works fine. Even the color flag is set.

Some hours ago i merged both applescripts to one and everything works fine!

But i'm sorry because for my extended workflows i need separate applescripts... ;(

Thanx again!

Thomas
t_klein
 
Posts: 20
Joined: Mon Feb 27, 2012 3:50 am

Check the logs then as it would seem that there was an error with the AppleScripts that stopped execution. Post any errors you find.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

@mr_noodle

Thank you again, but i found no error in every single script. the separated worked flawless.

So i decided to merge both to one.

Regards
Thomas
t_klein
 
Posts: 20
Joined: Mon Feb 27, 2012 3:50 am


Return to Support