AppleScript & Shell Script and back and forth

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

Moderator: Mr_Noodle

AppleScript & Shell Script and back and forth Tue Jun 26, 2018 11:22 pm • by dmayo2
Hey Group.

So, I have files that are named as such: "Blink013_30_18_6/02_AM_.mp4" which is the timestamp of the video. However, when the video is saved from the cloud servers locally, the created/modified date is the date it was downloaded. I'm trying to change the created and modified date to what's in the file name. [the above example is Blink01 -- camera name, then the date and time: March_30_2018 at 6:02am.

2 Issues

1. When I drag that file onto my terminal to change the created/modified dates, the terminal actually sees the file name as Blink013_30_18_6:02_AM_.mp4. I know that Finder doesn't like the colon, but is it actually an alias? Not sure, as I need to use the colon in my script as you'll see.
Code: Select all
setfile -d "3/30/2018 6:02 AM" -m "3/30/2018 6:02 AM" /Users/me/temp/Blink013_30_18_6:02_AM_.mp4

This works as expected when run in the terminal.

2. When I hard-cord the file as opposed to using theFile, the external AppleScript works fine, and even passes the new date back to Hazel as is evident via the logs: Hazel Alert: 3/30/2018 6/02 AM
[There's that slash instead of a colon.]

So, why isn't my script working when using theFile?

HARD-CODED filename
So, my script teases out the correct timestamp and passes it back to Hazel. The next step is to run a shell script that takes that date and runs the
Code: Select all
setfile
. All I get from the log files is
Code: Select all
Hazel Alert: 3/30/2018 6/02 AM
[Custom Message] Hazel Alert: 3/30/2018 6/02 AM
DEBUG: == script output ==
ERROR: invalid date/time

== End script output ==
[Error] Shell script failed: Error processing shell script on file /Users/me/temp/Blink013_30_18_6:02_AM_.mp4.
Shellscript exited with non-successful status code: 2



theFile filename
Here's the error I get when I use theFile instead of hard-coding the filename:
Code: Select all
[Error] AppleScript failed: Error executing AppleScript on file /Users/dmayo/temp/_deer/Blink013_30_18_6:02_AM_.mp4.
OSAScript error: {
    NSLocalizedDescription = "Can\U2019t get text 8 thru -1 of alias \"Macintosh HD:Users:me:temp:_deer:Blink013_30_18_6/02_AM_.mp4\".";
    NSLocalizedFailureReason = "Can\U2019t get text 8 thru -1 of alias \"Macintosh HD:Users:me:temp:_deer:Blink013_30_18_6/02_AM_.mp4\".";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: null()>";
    OSAScriptErrorBriefMessageKey = "Can\U2019t get text 8 thru -1 of alias \"Macintosh HD:Users:me:temp:_deer:Blink013_30_18_6/02_AM_.mp4\".";
    OSAScriptErrorMessageKey = "Can\U2019t get text 8 thru -1 of alias \"Macintosh HD:Users:me:temp:_deer:Blink013_30_18_6/02_AM_.mp4\".";
    OSAScriptErrorNumberKey = "-1728";
    OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'obj '{ 'form':'rang', 'want':'ctxt', 'seld':'rang'{ 'star':'obj '{ 'form':'indx', 'want':'ctxt', 'seld':8, 'from':'ccnt'($$) }, 'stop':'obj '{ 'form':'indx', 'want':'ctxt', 'seld':-1, 'from':'ccnt'($$) } }, 'from':'obj '{ 'want':'alis', 'from':null(), 'form':'name', 'seld':'utxt'(\"Macintosh HD:Users:me:temp:_deer:Blink013_30_18_6/02_AM_.mp4\") } }>";
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}
DEBUG: Tapping error retry sequence






Here's my Hazel:
upon match
Run AppleScript getFileDate.scpt [Options: export var fullDateTime]
Display notification with pattern •fullDateTime
Run shell script embedded script
Set color label red

Here's the embedded shell script:
Code: Select all
setfile -d fullDateTime -m fullDateTime $1


Here's the external AppleScript (again, if I use theFile, it fails, but if I hardcode the filename it works when running thru Hazel)
Code: Select all
on hazelProcessFile(theFile)
   
   -- set x to theFile
   set x to "Blink013_30_18_6/02_AM_.mp4"
   set xx to trim(x, 7)
   set xxx to trim(xx, -5)
   set xAr to splitText("_", xxx)
   
   set arDate to items 1 through 2 of xAr -- 3, 30
   set arYear to item 3 of xAr
   set arYear to addToList(arYear, 1, "20") -- 20, 1 8
   set theYear to joinList("", arYear)
   set arFullDate to addToList(arDate, 3, theYear)
   set fullDate to joinList("/", arFullDate) -- 3/30/2018
   
   set arTime to items 4 through 5 of xAr -- 6:02, AM
   set fullTime to joinList(" ", arTime) -- 6:02 AM
   set spacer to " "
   
   set fullDateTime to fullDate & spacer & fullTime -- 3/30/2018 6:02 AM
   
   return {hazelOutputAttributes:{fullDateTime}}
   
end hazelProcessFile

on trim(t, n)
   if n > 0 then
      return text (n + 1) thru -1 of t
   else
      return text 1 thru (n - 1) of t
   end if
end trim

on splitText(delimiter, someText)
   set prevTIDs to AppleScript's text item delimiters
   set AppleScript's text item delimiters to delimiter
   set output to text items of someText
   set AppleScript's text item delimiters to prevTIDs
   return output
end splitText

on addToList(theList, placeToInsert, whatToInsert)
   set tot to count of items of theList
   if placeToInsert is less than 1 then set placeToInsert to 1
   if placeToInsert is greater than tot then
      set end of theList to whatToInsert
      set NL to theList
   else
      set n to placeToInsert
      if placeToInsert is 1 then
         set lis1 to {whatToInsert}
      else
         set lis1 to {}
         repeat with i from 1 to n - 1
            set end of lis1 to item i of theList
         end repeat
         set end of lis1 to whatToInsert
      end if
      
      repeat with i from n to number of items in theList
         set end of lis1 to item i of theList
      end repeat
      set NL to lis1
   end if
   return NL
end addToList

on joinList(delimiter, someList)
   set prevTIDs to AppleScript's text item delimiters
   set AppleScript's text item delimiters to delimiter
   set output to "" & someList
   set AppleScript's text item delimiters to prevTIDs
   return output
end joinList


Any new directions for me to look would be welcomed. Thanks.
dmayo2
 
Posts: 8
Joined: Wed Mar 21, 2018 1:14 pm

Finder doesn't like colons but Terminal doesn't like slashes (/). When converting back and forth between the two, those characters get swapped.

Note that theFile is an alias. You'll need to get the POSIX path to be able to use it with a commandline program like SetFile.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Thanks for the direction. AppleScript issues solved, now just figuring out how to use the hazelOutputAttribute in the embedded shell script.

Ok, so I was able to convert the alias to text and now my script works (with the addition of splitting theFile)
Code: Select all
set xText to theFlie as text
set arX to splitText(":", xText)
set x to last item of arX

Now, x is just the file name without the path.
Great.

Next, that darn forward slash. So I found a sub-routine to replace_chars() and searched/replaced to the item list:
Code: Select all
-- new line of code
set item 4 of xAr to replace_chars(item 4 of xAR, "/", ":")
-- now xAR = 6:02, AM
-- continuing...
set arTime to items 4 through 5 of xAR
set fullTime to joinList(" ", arTime)


So, now the external script is handing the correct time formatted as setFile needs (3/30/2018 6:02 AM).
Great.


Ok, so the embedded shell script should be
Code: Select all
setfile -d fullDateTime -m fullDateTime $1



So, now I just need to be able to access the hazelOutputAttribute:{fullDateTime} in the embedded shell script. Hmm.
dmayo2
 
Posts: 8
Joined: Wed Mar 21, 2018 1:14 pm

All right. So Hazel can not give a shell script any attributes. Boo.

So, instead of returning a record as hazelOutputAttributes, I decided that I needed to write my fullDateTime to a file. After processing the theFile filename I added this code:

Code: Select all
set myFile to "setFileDate.txt"
set logpath to (system attribute "HOME") & "/temp/" as string
set logfile to logpath & myFile
set fd to open for access logfile with write permission
set eof of the fd to 0
write fullDateTime to fd
close access fd


So, now this txt file contains only the line 3/30/2018 6:02 AM and everytime this AppleScript runs, it replaces the contents of this file rather appending it.

Now to alter the embedded shell script. I'm pretty sure that because my txt file is in the same directory that Hazel is watching, I don't need to put a full path in my cat.
Code: Select all
value=$(<setFileDate.txt)
setfile -d "$value" -m "$value" $1
exit 0


Thanks again Mr Noodle for the direction. Hazel saving me hours again. :D
dmayo2
 
Posts: 8
Joined: Wed Mar 21, 2018 1:14 pm


Return to Support