Hi
Firstly apologies as I am very new to scripting and may be asking some very simple questions.
I’m wondering if it is possible to set up a script that takes a section of a text in textedit and can then effectively paste that over another section. i need it to repeat this every 4 lines.
here an is an excerpt from the text file im trying to fix
001 AX V C 00:00:00:00 00:02:54:01 10:00:02:00 10:02:56:01
- FROM CLIP NAME: FLAME_ELEMENTS_DAY_01_SHOT_01_HD
- PROBLEM WITH EDIT: CLIP HAD NO TIMECODE TRACK.
002 AX V C 00:00:00:00 00:02:41:06 10:02:57:01 10:05:38:07
- FROM CLIP NAME: FLAME_ELEMENTS_DAY_01_SHOT_02_HD
- PROBLEM WITH EDIT: CLIP HAD NO TIMECODE TRACK.
What im trying to do is replace the 1st and 2nd string of numbers with the 3rd and 4th. and then repeat that every 4 lines.
currently what I have is
tell application "TextEdit"
tell text of document of front window
repeat with P from (count of paragraphs) to 4 by -4
set TC to {characters 53 through 76} of P
write TC to character 30 of paragraph
end repeat
end tell
end tell
This is not working but i cant figure out what’s going wrong.
any help would be appreciated.
Thanks.
You want to delete 00:00:00:00 00:02:54:01 ?
Give this a try and see if you can figure out how it works! I put comments into the code to help you.
The code basically takes a line like this…
"002 AX V C 00:00:00:00 00:02:41:06 10:02:57:01 10:05:38:07 "
and makes it look like this by removing the first two number strings from a line…
“002 AX V C 10:02:57:01 10:05:38:07”
-- get the text of the text edit's front document
tell application "TextEdit"
set t to text of document 1
end tell
-- convert the text into a list and modify it as required
set pt to paragraphs of t
set newList to pt
repeat with i from 1 to count of pt
set thisLine to item i of pt
-- we identify the proper line to adjust if it starts with a number and also contains a colon
try
(first word of thisLine) as number
if thisLine contains ":" then
set item i of newList to removeNumberStrings1and2(thisLine)
end if
end try
end repeat
-- convert the modified list back into text
set {tids, text item delimiters} to {text item delimiters, return}
set newString to newList as text
set text item delimiters to tids
-- put the new text back into text edit
tell application "TextEdit"
set text of document 1 to newString
end tell
(*================== SUBROUTINES =================*)
on removeNumberStrings1and2(theLine)
try
-- convert the line into a list
set {tids, text item delimiters} to {text item delimiters, space}
set ti to text items of theLine
-- find the position of the first number string to replace e.g. it contains ":"
repeat with j from 1 to count of ti
if item j of ti contains ":" then exit repeat
end repeat
-- knowing the position of the first number string we can make the adjustments
-- here we are just removing the first two number strings
set finalTI to (items 1 thru (j - 1) of ti) & (items (j + 2) thru end of ti)
-- convert the list back into a string
set newLine to finalTI as text
set text item delimiters to tids
return newLine
on error
set text item delimiters to ""
return theLine
end try
end removeNumberStrings1and2
Here is another approach:
do shell script "cat /Users/struanf/Desktop/yourfile.txt | sed 's/..:..:..:.. ..:..:..:.. \\(..:..:..:.. ..:..:..:..\\)/\\1/'g"
Hi. If the text example were properly formatted, this would work:
tell application "TextEdit"'s document 1
tell its text to repeat with counter from ((count paragraphs) - 2) to 1 by -4
delete paragraph counter's characters 30 thru 53
end repeat
end tell
The text is currently gremlin-ridden with wonky line endings that result in it being one giant paragraph. A good text editor could fix much of that for you, natively. I recommend TextWrangler, which is free and has both great Applescript support and a comprehensible dictionary, unlike TextEdit, which appears to have been written by flying monkeys.
Thank you very much for all the help. apologies for the late response sidetracked by other projects.
I ended up using regulus6633 solution which worked very well i made a very minor tweak to repeat the string twice. Still cant my head around all of it but i look forward to it making more sense the more I learn.
-- get the text of the text edit's front document
tell application "TextEdit"
set t to text of document 1
end tell
-- convert the text into a list and modify it as required
set pt to paragraphs of t
set newList to pt
repeat with i from 1 to count of pt
set thisLine to item i of pt
-- we identify the proper line to adjust if it starts with a number and also contains a colon
try
(first word of thisLine) as number
if thisLine contains ":" then
set item i of newList to removeNumberStrings1and2(thisLine)
end if
end try
end repeat
-- convert the modified list back into text
set {tids, text item delimiters} to {text item delimiters, return}
set newString to newList as text
set text item delimiters to tids
-- put the new text back into text edit
tell application "TextEdit"
set text of document 1 to newString
end tell
(*================== SUBROUTINES =================*)
on removeNumberStrings1and2(theLine)
try
-- convert the line into a list
set {tids, text item delimiters} to {text item delimiters, space}
set ti to text items of theLine
-- find the position of the first number string to replace e.g. it contains ":"
repeat with j from 1 to count of ti
if item j of ti contains ":" then exit repeat
end repeat
-- knowing the position of the first number string we can make the adjustments
-- here we are just removing the first two number strings
set finalTI to (items 1 thru (j - 1) of ti) & (item (j + 2) of ti) & (item (j + 3) of ti) & (items (j + 2) thru end of ti)
-- convert the list back into a string
set newLine to finalTI as text
set text item delimiters to tids
return newLine
on error
set text item delimiters to ""
return theLine
end try
end removeNumberStrings1and2
I didn’t really make it clear that the need to repeat the string twice eg
001 AX V C 00:00:00:00 00:02:54:01 10:00:02:00 10:02:56:01
- FROM CLIP NAME: FLAME_ELEMENTS_DAY_01_SHOT_01_HD
- PROBLEM WITH EDIT: CLIP HAD NO TIMECODE TRACK.
002 AX V C 00:00:00:00 00:02:41:06 10:02:57:01 10:05:38:07
- FROM CLIP NAME: FLAME_ELEMENTS_DAY_01_SHOT_02_HD
- PROBLEM WITH EDIT: CLIP HAD NO TIMECODE TRACK.
would change too
001 AX V C 10:00:02:00 10:02:56:01 10:00:02:00 10:02:56:01
- FROM CLIP NAME: FLAME_ELEMENTS_DAY_01_SHOT_01_HD
- PROBLEM WITH EDIT: CLIP HAD NO TIMECODE TRACK.
002 AX V C 10:02:57:01 10:05:38:07 10:02:57:01 10:05:38:07
- FROM CLIP NAME: FLAME_ELEMENTS_DAY_01_SHOT_02_HD
- PROBLEM WITH EDIT: CLIP HAD NO TIMECODE TRACK.
Thank you again for the help.
That’s a simple fix to make it how you want. As mentioned, we know the position of the first number string so I just adjusted what happened once we reached that point in the script. This will work with no repeats. And good luck understanding the code. It’s really not complex but probably seems that way to a newbie
-- get the text of the text edit's front document
tell application "TextEdit"
set t to text of document 1
end tell
-- convert the text into a list and modify it as required
set pt to paragraphs of t
set newList to pt
repeat with i from 1 to count of pt
set thisLine to item i of pt
-- we identify the proper line to adjust if it starts with a number and also contains a colon
try
(first word of thisLine) as number
if thisLine contains ":" then
set item i of newList to replaceNumberStrings1and2With3and4(thisLine)
end if
end try
end repeat
-- convert the modified list back into text
set {tids, text item delimiters} to {text item delimiters, return}
set newString to newList as text
set text item delimiters to tids
-- put the new text back into text edit
tell application "TextEdit"
set text of document 1 to newString
end tell
(*================== SUBROUTINES =================*)
on replaceNumberStrings1and2With3and4(theLine)
try
-- convert the line into a list
set {tids, text item delimiters} to {text item delimiters, space}
set ti to text items of theLine
-- find the item number of the first number string
repeat with j from 1 to count of ti
if item j of ti contains ":" then exit repeat
end repeat
-- using the known position "j", we can make the substitutions as needed
set item j of ti to item (j + 2) of ti
set item (j + 1) of ti to item (j + 3) of ti
-- convert the list back into a string
set newLine to ti as text
set text item delimiters to tids
return newLine
on error
set text item delimiters to ""
return theLine
end try
end replaceNumberStrings1and2With3and4