edit lines of text coming from "filter Paragraphs" action on automator

Hello, i’m very new to scripting and automator so thank you for taking my newbie questions.

I filtered a text file, and am getting it separated by the parts of text I need, and then joined again as I need.
For this, I’m using automator actions and it’s working.
But before each part is saved I need to clean some more and I’m wondering if a script after “filter paragraphs” action and before “new text file” action would do the thing.

On 01.txt:
I need this
SESSION NAME: Yaddayaddayadda
SESSION NAME: Yaddayaddayadda

to become
SESSION NAME: Yaddayaddayadda

on 02.txt:
from this
CHANNEL EVENT CLIP NAME START TIME END TIME DURATION STATE
CHANNEL EVENT CLIP NAME START TIME END TIME DURATION STATE
CHANNEL EVENT CLIP NAME START TIME END TIME DURATION STATE

to this
CLIP NAME START TIME END TIME DURATION

the number of lines can be from one to hundreds, I need only the first one

on 03.txt
from this
1 2 whoosh 003_.L 00:03:52:21 00:03:53:18 00:00:00:21 Unmuted
1 2 whoosh 003_.L 00:03:52:21 00:03:53:18 00:00:00:21 Unmuted
1 2 whoosh 003_.L 00:03:52:21 00:03:53:18 00:00:00:21 Unmuted
1 2 whoosh 003_.L 00:03:52:21 00:03:53:18 00:00:00:21 Unmuted

to this
whoosh 003_.L 00:03:52:21 00:03:53:18 00:00:00:21

these lines can be hundreds, I need them all but without the first numbers and spaces and last word “Unmuted”

Thank you so much

Hi samh. Welcome to MacScripter.

If I’ve understood you correctly, the following AppleScript should cover all three cases. The “Run AppleScript” action containing it should go immediately after the “Filter Paragraphs” action(s). Note that it edits any line beginning with “CHANNEL” or a number, so if that’s not suitable in all cases, it’ll need to be modified or rewritten as separate scripts. Its output is a single text, but if you want the output as separate lines, like the output from “Filter Paragraphs”, there’s a note in the script about how to get that.

(* Filter out duplicate paragraphs. Edit those beginning with "CHANNEL" or a number. *)

-- input: list of paragraphs returned by the Automator "Filter Paragraphs" action.
-- parameters: not used.
-- output: result as a single text.
on run {input, parameters}
	set output to {}
	repeat with thisLine in input
		if ((thisLine begins with "CHANNEL") or (character 1 of thisLine is in "0123456789")) then
			set thisLine to text from word 3 to word -2 of thisLine
		end if
		if (thisLine is not in output) then set end of output to thisLine as text
	end repeat
	
	-- The following four lines convert the list of output lines to a single text.
	-- Delete them if not wanted.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to linefeed
	set output to output as text
	set AppleScript's text item delimiters to astid
	
	return output
end run

Hello Nigel,
Thank you so much for your reply.

when I put the script on a “run applescript” action it returns no erros, but running the automator I get this,
!The action “Run AppleScript” encountered an error: “Can’t make character 1 of item 1 of {current application, current application, current application, current application, current application, current application, current application} into type Unicode text.”!

and there is no output

could you help me with what I’m doing wrong?

thank you so much

Hi samh.

Are you placing the “Run AppleScript” action immediately after “Filter Paragraphs”? As far as I can see, and according to my tests this morning, the latter’s supposed to return a list of texts representing the filtered paragraphs (or lines in your case). Whatever’s being reported as ‘current application’ certainly isn’t text!

What’s your input to “Filter Paragraphs”? It works for me both with a complete text and with a list of individual paragraphs.

I managed to make it work but not on my main automator.
I think the mistake is because I’m receiving to filter, text from the clipboard.

could this be the case?
How do I fix it?

Thank you

It only works after a filter text thing,

but if the text to be filtered comes from clipboard, it fails.

Hello Nigel,

Thank you so much, I changed my workflow to allow the script to work properly.

It’s now doing everything

Thank you

Hi samh.

I’m glad you’ve got something that’s working for you. It does seem to be the clipboard actions that were causing the problem. It’s not usually necessary to use the clipboard in an automated process, but the same happens when Automator’s variables are used instead. I’m pretty sure it’s a bug. An action which recognises and filters paragraphs should be passing text to the next action in the sequence! :confused:

Hello Nigel,

I’m trying to simplify the workflow I made once I noticed how clean your script makes everything.
Is it possible to add to the script delete every line starting with:

SAMPLE RATE:
BIT DEPTH:
SESSION START TIMECODE (there is another line starting with SESSION that can’t be deleted)
TIMECODE FORMAT:

OF AUDIO TRACKS:

OF AUDIO CLIPS:

OF AUDIO FILES:

T R A C K L I S T I N G
TRACK NAME:
COMMENTS:
USER DELAY:
STATE:

Thank you so much

Hi samh.

I think this covers all eventualities (Automator bugs and non-text input excepted). The input from the preceding action can be either a whole text or a list of its paragraphs and you can edit the “unwanteds” list if you need to make any changes there. Everything else is as before.

on run {input, parameters}
	-- Automator's "Run AppleScript" actions appear always to receive their input in list form.
	-- If the input here contains only one item, and it's text, get a list of the text's 'paragraphs'.
	if (((count input) is 1) and (class of item 1 of input is text)) then
		set input to paragraphs of item 1 of input
	end if
	
	set unwanteds to {"SAMPLE RATE:", "BIT DEPTH:", "SESSION START TIMECODE", ¬
		"TIMECODE FORMAT:", "# OF AUDIO TRACKS:", "# OF AUDIO CLIPS:", ¬
		"# OF AUDIO FILES:", "T R A C K  L I S T I N G", "TRACK NAME:", ¬
		"COMMENTS:", "USER DELAY:", "STATE:"}
	
	set output to {}
	repeat with thisLine in input
		set OKSoFar to true
		repeat with thisBeginning in unwanteds
			if (thisLine begins with thisBeginning) then
				set OKSoFar to false
				exit repeat
			end if
		end repeat
		if (OKSoFar) then
			if ((thisLine begins with "CHANNEL") or (character 1 of thisLine is in "0123456789")) then
				set thisLine to text from word 3 to word -2 of thisLine
			end if
			if (thisLine is not in output) then set end of output to thisLine as text
		end if
	end repeat
	
	-- The following four lines convert the list of output lines to a single text.
	-- Delete them if not wanted.
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to linefeed
	set output to output as text
	set AppleScript's text item delimiters to astid
	
	return output
end run

Hello Nigel,
so far its looking great,

Thank you so so much!!

Hello Nigel,
Here’s a link to the hole workflow you helped me with.
Thank you so much.
https://www.facebook.com/groups/soundflow/permalink/773753513465810/