Using shell script in embedded AppleScript

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

Moderator: Mr_Noodle

Using shell script in embedded AppleScript Mon Sep 07, 2020 5:54 pm • by Frank
I'm trying to get SHA1 hashes from certain files. My embedded AppleScript is as follows:
Code: Select all
set shasum to do shell script "/usr/bin/shasum -a1 " & theFile
set sha1 to text 1 thru 40 of shasum
return {hazelPassesScript:true, hazelOutputAttributes:sha1}


But when I run it, it throws the following error:

2020-09-07 23:47:48.959 hazelworker[11302] payments-noreply_google.com.pdf: Rule SHA1 matched.
2020-09-07 23:47:48.959 hazelworker[11302] DEBUG: Manual run. Forcing actions to be executed.
2020-09-07 23:47:49.133 hazelworker[11302] [Error] AppleScript failed: Error executing AppleScript on file /Volumes/sqooshdisk/88_hazel-test/payments-noreply_google.com.pdf.
2020-09-07 23:47:49.134 hazelworker[11302] OSAScript error: {
NSLocalizedDescription = "shasum: sqooshdisk:88_hazel-test:payments-noreply_google.com.pdf: ";
NSLocalizedFailureReason = "shasum: sqooshdisk:88_hazel-test:payments-noreply_google.com.pdf: ";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,2 \"hazelworker\"]>";
OSAScriptErrorAppNameKey = "Hazel.prefPane";
OSAScriptErrorBriefMessageKey = "shasum: sqooshdisk:88_hazel-test:payments-noreply_google.com.pdf: ";
OSAScriptErrorMessageKey = "shasum: sqooshdisk:88_hazel-test:payments-noreply_google.com.pdf: ";
OSAScriptErrorNumberKey = 1;
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
2020-09-07 23:47:49.134 hazelworker[11302] DEBUG: Tapping error retry sequence
2020-09-07 23:47:49.136 hazelworker[11302][PREDICTION] DEBUG: Calculating fire time - predicate: typeObject isType: "public.content" result: 0
2020-09-07 23:47:49.136 hazelworker[11302][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2020-09-07 23:47:49.136 hazelworker[11302][PREDICTION] DEBUG: Bail out: AND predicate
2020-09-07 23:47:49.137 hazelworker[11302][PREDICTION] DEBUG: Predicted fire time for file: /Volumes/sqooshdisk/88_hazel-test/tmp and rule SHA1: 4001-01-01 01:00:00.000 Should poll: 0
2020-09-07 23:47:49.137 hazelworker[11302] DEBUG: tmp: Did not match any rules.
2020-09-07 23:47:49.137 hazelworker[11302] DEBUG: Writing out DB file for /Volumes/sqooshdisk/88_hazel-test to path: /Users/sqoosh/Library/Application Support/Hazel/16777226-885965.hazeldb


Could really use some pointers.
Frank
 
Posts: 3
Joined: Mon Sep 07, 2020 5:41 pm

Have you tried running the script outside of Hazel?
Mr_Noodle
Site Admin
 
Posts: 11226
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Using shell script in embedded AppleScript Tue Sep 08, 2020 12:36 pm • by Frank
Mr_Noodle wrote:Have you tried running the script outside of Hazel?

Yes, I tried the script in Scripteditor.app prepended by
Code: Select all
set theFile to "/Volumes/{{local file}}.pdf
. That works fine.
Frank
 
Posts: 3
Joined: Mon Sep 07, 2020 5:41 pm

Is that the same file being processed by Hazel? Also, there's a missing quote at the end of your example which reminds me that you may need to add quotes in your shell script around "theFile". You would probably need to escape them so that the AppleScript doesn't absorb them.
Mr_Noodle
Site Admin
 
Posts: 11226
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Using shell script in embedded AppleScript Fri Sep 11, 2020 4:00 pm • by Frank
I managed to fix it another way. First, I take the file's full path as an inputAttribute. Then I feed that into the Shell script command. No need for escaped quotes and all that.

Code: Select all
set theFile to item 1 of inputAttributes
set sha1 to do shell script "/usr/bin/shasum -a1 " & theFile & "  | cut -d' ' -f1"
return {hazelPassesScript:true, hazelOutputAttributes:{sha1}}
Frank
 
Posts: 3
Joined: Mon Sep 07, 2020 5:41 pm


Return to Support