-- Applescript 1.1, uses Jon's Commands osax -- converts standard tab on the clipboard to fripp tab -- second-stage program tk will optimize the fripp tablature. -- on run -- initialize set inTab to (the clipboard) as text global outTab set outTab to {"", "", "", "", "", ""} set inLines to number of paragraphs in inTab set standardNotes to {"E", "B", "G", "D", "A", "E"} set frippNotes to {"G", "E", "A", "D", "G", "C"} set deltaTable to {-3, -5, -2, 0, 2, 4} -- check to see if it's really tab if inLines ­ 7 then display dialog "Sorry, that doesn't look like tab to me. Error code " & inLines stop else repeat with i from 1 to 6 -- check for note prefix set theLine to paragraph i of inTab if character 1 of theLine is item i of standardNotes then set lineLength to length of theLine set theLine to ((item i of frippNotes) as string) & (characters 2 through lineLength of theLine) else set theLine to ((item i of frippNotes) as string) & "|" & theLine end if copy theLine to item i of outTab -- display (item i of outTab) as string --for debug end repeat end if -- map the notes -- ignore the last line, which should be blank set inLines to 6 repeat with i from inLines to 1 by -1 -- mapping function set theLine to item i of outTab set theDelta to item i of deltaTable if theDelta ­ 0 then set wordCount to number of words in theLine set lastOffset to 1 --display dialog theLength -- for debug repeat with j from 1 to wordCount --display dialog (word j of theLine) -- for debug -- is it a number? set isNum to true try set theNote to (word j of theLine) as number on error set isNum to false end try if isNum is true then set theLength to number of characters in theLine set moveNote to false set noteLength to length of (word j of theLine) set newNote to (theNote + theDelta) if newNote < 0 then set newNote to (newNote + 7) set moveNote to true end if -- write the note to the output tab set newNote to newNote as string set theOffset to (lastOffset + (offset of theNote in  (characters lastOffset through theLength of theLine) as string) - 1) set lastOffset to theOffset + 1 if moveNote is false then if (length of newNote) < length of (theNote as string) then  set newNote to "-" & newNote if (length of newNote) > length of (theNote as string) then repeat with k from 1 to 6 if k ­ i then set oldLine to (item k of outTab) as string set newLine to replaceText("-", oldLine, 0, theOffset) copy newLine to item k of outTab --display dialog newLine --for debug end if end repeat end if set theLine to replaceText(newNote, theLine, noteLength, theOffset) --display dialog "Moved " & theNote & " to " & newNote -- for debug --display dialog theLine -- for debug else --display dialog "moveNote is true" -- for debug -- is there a note at the same spot on the next lower line? -- if so, ask if we should overwrite it. set targetLine to (item (i + 1) of outTab) as string set targetFlag to false if theOffset > 0 then if theOffset < theLength then set theTarget to (characters (theOffset - 1) through  (theOffset + 1) of theLine) as string else set theTarget to (characters (theOffset - 1) through  (theOffset) of theLine) as string end if else set theTarget to (characters (theOffset) through  (theOffset + 1) of theLine) as string end if try set theTarget to (word 1 of theTarget) as number on error set targetFlag to true end try -- no conflict if targetFlag is true then if length of newNote = 1 then set bufferText to "--" else set bufferText to "-" end if repeat with k from 1 to 6 set oldLine to (item k of outTab) as string if k ­ i then set newLine to replaceText(bufferText, oldLine, 0, theOffset) else set newLine to replaceText(bufferText, oldLine, (length of bufferText), theOffset) end if copy newLine to item k of outTab --display dialog newLine --for debug end repeat -- conflict! else display dialog "Overwrite existing note " & theTarget &  " from line " & (item (i + 1) of outTab) & " with " & newNote &  "?" buttons {"Yes", "No"} if length of newNote = 1 then set bufferText to "--" else set bufferText to "-" end if repeat with k from 1 to 6 set oldLine to (item k of outTab) as string if k ­ i then set newLine to replaceText(bufferText, oldLine, 0, theOffset) else set newLine to replaceText(bufferText, oldLine, (length of bufferText), theOffset) end if copy newLine to item k of outTab --display dialog newLine --for debug end repeat end if end if else -- leave it as is if it isn't a number end if --display dialog theLine --for debug end repeat -- word by word (j) end if copy theLine to item i of outTab --display dialog item i of outTab --for debug end repeat -- line by line (i) set finalTab to "" repeat with i from 1 to 6 set finalTab to finalTab & (item i of outTab) & return end repeat set the clipboard to finalTab end run on replaceText(newText, oldLine, oldtextLength, newtextOffset) set oldLength to length of oldLine if (newtextOffset - 1) ­ 0 then set notePrefix to (characters 1 through (newtextOffset - 1) of oldLine) as string else set notePrefix to "" end if if (newtextOffset + oldtextLength) > oldLength then set noteSuffix to "" else set noteSuffix to ((characters (newtextOffset + oldtextLength) through oldLength)  of oldLine) as string end if --display dialog notePrefix as string -- for debug --display dialog noteSuffix as string -- for debug return (notePrefix & newText & noteSuffix) as string end replaceText