Page 1 of 1

Embedded script -- no error, but not running?

PostPosted: Tue Sep 30, 2025 8:44 pm
by AndrewOfTheForest
First and foremost: love, Love, LOVE Hazel!!!!!!!!!! My deepest gratitude to its creator!

Below is my first embedded script.

I've used the Preview Rule feature to firm my file matches, and the View Logs feature confirms the match, as well. However, my embedded script doesn't seem to be running:

Code: Select all
-- Hazel Embedded AppleScript (no custom handlers)
-- 1) Remove all lines through the first blank line
-- 2) Remove every "{comment:" and "}"
-- 3) Remove everything from (and including) "CCLI Song" to the end
-- Overwrites the original file (theFile)

-- Hazel supplies `theFile` (an alias) to this embedded script
set inputFile to theFile

-- Read file as UTF-8
set fileRef to open for access inputFile
try
   set fileText to (read fileRef as «class utf8»)
   close access fileRef
on error eMsg number eNum
   try
      close access fileRef
   end try
   error eMsg number eNum
end try

-- Keep a backup of current delimiters
set prevTID to AppleScript's text item delimiters

-- 1) Remove everything through the first blank line
set AppleScript's text item delimiters to ""
set textLines to paragraphs of fileText

set cutoffIndex to 0
repeat with i from 1 to (count of textLines)
   if item i of textLines is "" then
      set cutoffIndex to i
      exit repeat
   end if
end repeat

if cutoffIndex is not 0 then
   set remainingLines to items (cutoffIndex + 1) thru -1 of textLines
else
   set remainingLines to textLines
end if

-- Re-join to text (normalize to LF)
set AppleScript's text item delimiters to linefeed
set newText to remainingLines as text
set AppleScript's text item delimiters to ""

-- 2) Strip "{comment:" and "}" (simple global replace via delimiters)
-- Remove "{comment:"
set AppleScript's text item delimiters to "{comment:"
set newText to (text items of newText) as text
set AppleScript's text item delimiters to ""

-- Remove "}"
set AppleScript's text item delimiters to "}"
set newText to (text items of newText) as text
set AppleScript's text item delimiters to ""

-- 3) Remove everything from (and including) "CCLI Song" to the end
set AppleScript's text item delimiters to "CCLI Song"
set parts to text items of newText
if (count of parts) > 1 then
   set newText to item 1 of parts
end if

-- Restore original delimiters
set AppleScript's text item delimiters to prevTID

-- Overwrite the original file
set fileRef to open for access inputFile with write permission
try
   set eof of fileRef to 0
   write newText as «class utf8» to fileRef
   close access fileRef
on error eMsg number eNum
   try
      close access fileRef
   end try
   error eMsg number eNum
end try


Any suggestions????

Re: Embedded script -- no error, but not running?

PostPosted: Wed Oct 01, 2025 8:53 am
by Mr_Noodle
Have you tried the script outside of Hazel?

Re: Embedded script -- no error, but not running?

PostPosted: Wed Oct 01, 2025 8:42 pm
by AndrewOfTheForest
Thank you for taking the time to respond! It seems to be working now.