Applescript trouble

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

Moderator: Mr_Noodle

Applescript trouble Sun Feb 12, 2017 8:16 pm • by deathrobot
Hello. I am using a applescript that plays a random album in iTunes. It works fine on its own but when calling it as an external script in a rule, it fails. From searching around this forum I understand that this may be a limitation of Hazel based on having a handler within a handler. Is there a way around this limitation (for example, by changing elements of the applescript)? Or will this particular task not be possible with Hazel? Here's the applescript code:

Code: Select all
property randomAlbumName : "[-Some Random Album>"
property minimumTracksRequired : 5

set spareTracks to {}

tell application "iTunes"
   repeat
      try
         set someTrack to some track of (some playlist whose special kind is music) whose album is some item of my getAlbums()
         set theAlbumTracks to tracks whose album is (get album of someTrack) and disc number is (get disc number of someTrack)
         if length of theAlbumTracks ≥ minimumTracksRequired then exit repeat
      end try
   end repeat
   
   try
      set myRandomPlaylist to user playlist randomAlbumName
      delete every track of myRandomPlaylist
   on error
      set myRandomPlaylist to make new playlist with properties {name:randomAlbumName} -- , shuffle:true}
   end try
   
   repeat with n from 1 to length of theAlbumTracks
      set chk to false
      repeat with thisTrack in theAlbumTracks
         try
            if track number of thisTrack = n then
               set chk to true
               (my copyTrack:thisTrack toPlaylist:myRandomPlaylist)
               exit repeat
            end if
         end try
      end repeat
      if (chk is false) then set end of spareTracks to thisTrack
   end repeat
   
   if spareTracks is not {} then
      repeat with thisTrack in spareTracks
         (my copyTrack:thisTrack toPlaylist:myRandomPlaylist)
      end repeat
   end if
   play myRandomPlaylist
   try
      reveal myRandomPlaylist
   end try
   activate
end tell

to copyTrack:aTrack toPlaylist:thePlaylist
   tell application "iTunes"
      try
         if not (exists (some track of thePlaylist whose database ID is (get aTrack's database ID))) then
            duplicate aTrack to thePlaylist
         end if
      on error m
         log m
      end try
   end tell
end copyTrack:toPlaylist:

to getAlbums()
   set theCommand to "perl -e 'local $/=undef;my $s=<>;my @al=();my %seen=();while ($s=~m:<key>Album</key><string>(.*?)</string>:sg){$g = $1;$g=~ s:\\&\\#(\\d*);:chr($1):ge;$g=($g.\"" & (ASCII character 198) & "\");push(@al,$g)unless $seen{$g}++;}print @al;
' " & theDatabase()
   set g to textToList((do shell script theCommand), (ASCII character 198))
   if last item of g is "" then set g to items 1 thru -2 of g
   return g
end getAlbums

on theDatabase()
   try
      set x to (do shell script "perl -e'open(T,\"defaults read com.apple.iApps iTunesRecentDatabasePaths|\");while(<T>){$t.= $_;}close(T);$t=~m|\"(.*?)\"|s;$t=$1;$t=~s| |\\\\ |g;print $t;'")
      return quoted form of (do shell script "ls " & replaceChars(escapeForUnix(x), "'", "\\'"))
   on error m
      log m
   end try
end theDatabase

on replaceChars(txt, srch, repl)
   set text item delimiters to the srch
   set the item_list to every text item of txt
   set text item delimiters to the repl
   set txt to the item_list as string
   set text item delimiters to ""
   return txt
end replaceChars

on textToList(txt, delim)
   set saveD to text item delimiters
   try
      set text item delimiters to {delim}
      set theList to every text item of txt
   on error errStr number errNum
      set text item delimiters to saveD
      error errStr number errNum
   end try
   set text item delimiters to saveD
   return (theList)
end textToList

to escapeForUnix(n)
   set badlist to "!@#$%^&*()+=-{}[]:;?<>"
   set filled to ""
   repeat with i from 1 to (length of n)
      set t to (text i of n)
      if t is in badlist then
         set filled to (filled & "\\" & t)
      else
         set filled to (filled & t)
      end if
   end repeat
   return filled
end escapeForUnix
deathrobot
 
Posts: 10
Joined: Sat Feb 07, 2015 9:53 pm

Re: Applescript trouble Mon Feb 13, 2017 12:29 pm • by Mr_Noodle
You'll need to use an external script. Either that or re-structure the script so that it doesn't have extra handlers.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Applescript trouble Mon Feb 13, 2017 7:32 pm • by deathrobot
I am using an external script, not embedding the code.
deathrobot
 
Posts: 10
Joined: Sat Feb 07, 2015 9:53 pm

Re: Applescript trouble Tue Feb 14, 2017 12:55 pm • by Mr_Noodle
Then the error lies elsewhere. Check the logs to see what the error is and report back.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Applescript trouble Tue Feb 14, 2017 10:19 pm • by deathrobot
Thanks. All looks well in the log file (rules matched, etc.) but the applescript causes this error:

2017-02-12 15:54:57.607 hazelworker[67113] [Error] AppleScript failed: Error executing AppleScript on file /Users/spidermusicserver/Dropbox/Mac Mini Control/#random.txt.
2017-02-12 15:54:57.607 hazelworker[67113] OSAScript error: {
OSAScriptErrorNumberKey = "-1708";
}

Apple describes this error as "The script doesn’t understand the <message> message. The event was not handled." From other Google results, it does seem like it's some sort of handler issue. I'm just not sure how to solve it.
deathrobot
 
Posts: 10
Joined: Sat Feb 07, 2015 9:53 pm

Re: Applescript trouble Wed Feb 15, 2017 12:23 pm • by Mr_Noodle
Ah, should have noticed this earlier. When it's an external script, you have to provide a special handler for Hazel to call. Search the help for "AppleScript" as it has more details there.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Applescript trouble Wed Feb 15, 2017 12:44 pm • by deathrobot
I saw that you mentioned that in a similar post to mine. Where exactly is the Help section? I've searched for 'applescript' under "Support" on the main Noodlesoft page (just comes up with Release Notes and an OmniFocus script article), under the "?" icon in the app itself, through Google… and am not coming up with anything about Hazel handlers for external scripts. Thanks!
deathrobot
 
Posts: 10
Joined: Sat Feb 07, 2015 9:53 pm

Re: Applescript trouble Thu Feb 16, 2017 12:54 pm • by Mr_Noodle
The ? button in the app should work as well as the Help menu there. Search for "AppleScript".
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support