Copy incremental tags from text and paste to document

Try this! :slight_smile:

Come back if it doesn’t work as expected!


property scriptTitle : "TagInserter"
set tagl to {{leadingnumber(0) & "<0/>"}, {leadingnumber(1) & "<1/>"}, {leadingnumber(2) & "<2/>"}, {leadingnumber(3) & "<3/>"}, {leadingnumber(4) & "<4/>"}, {leadingnumber(5) & "<5/>"}, {leadingnumber(6) & "<6/>"}, {leadingnumber(7) & "<7/>"}, {leadingnumber(8) & "<8/>"}, {leadingnumber(9) & "<9/>"}}


set res to choosesomething("Choose tag to insert", tagl, item 1 of tagl)

if res = false then return
set the clipboard to (characters 4 thru -1 of (res as text)) as text as record

tell application "System Events" to tell application process "OmegaT"
	set frontmost to true
	keystroke "v" using {command down, option down, shift down}
end tell

on leadingnumber(n)
	return text 1 thru 3 of (n & "   " as text)
end leadingnumber

on choosesomething(aPrompt, listOfSomething, defaultSomething)
	local theChoice
	tell application "SystemUIServer"
		activate
		set theChoice to choose from list listOfSomething with prompt aPrompt with title my scriptTitle default items defaultSomething without multiple selections allowed and empty selection allowed
	end tell
	return theChoice
end choosesomething

Ok, this is really weird.

Using your script, OmegaT doesn’t seem to see them as tags, but rather as characters, so instead of seeing a finished sentence of:

This is a sentence.

I get:

This is a sentence<0/>.

Don’t know why that is. That’s really very strange.

Hello!

I think that is because the text isn’t pure ascii, but contains formatting information in addition to the tags.

I think I’ll make another part of the script, which is run the first time, where you “collect” tags from 0 to 9 from the omegaT document, which you then can paste back by the program later.

Because if you get the tag from the omegaT document, and then paste back, then it will work, won’t it?

Could you please try that?

My idea is that you have a document with every tag, you copy them to clipboard by cmd C, run the script, and the script will store them sequentially, with a message about which tag being stored.
when every tag is stored, the script will enter production and lets you paste the stored tags.

What do you think about that?

When you “Register” the tags from 0-9 you will have to run the script from the script menu, as quicksilver doesn’t save the state of the script after it is run.

The second you go in and edit the script afterwards,you will loose everything

I recommend, having a document with the 10 tags, copy tag 0 to the clipboard, run the script, copy tag 1, run the script, until you have all 10 tags.

Yes, that’s the normal way of handling them.

Do you mean copy the entire sentence or each tag in the sentence individually?

If you mean the first option, that would be great. There is already a file that copies and stores the source and target sentence for each segment a translator works with so that a certain degree of text manipulation can be done.

If you mean copying the tags individually, I think that would be far slower than entering them by hand.

Regards,

Bowjest

Hello I mean copying the tags with formatting information once as the script starts up.

Then you have to run the script from the script menu and copy the tags <0/> thru <9/> with the formatting up front, run the script, until you have “recorded” the tags, then the script enters production mode

After that, you run the script from quicksilver and paste it in, and hopefully that will work.

I have used this with bold text in TextEdit, and it retains the bold formatting, the problem with the script above was that I coerced the clipboard to the datatype text, I have now changed that to a record, which then should as I understand it, adapt to the formatting already in the text in this special case. So the quick fix, is to try the script above, edited with my new understanding.

If that doesn’t work, try the script below. :slight_smile:


property scriptTitle : "TagInserter"

property hasRecorded : false
property tags : {missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value, missing value}


if not hasRecorded then
	set foundmissing to false
	set i to 0
	repeat with atag in tags
		if contents of atag is missing value then
			set foundmissing to true
			try
				display dialog "Tag nr " & i & " will be inserted into the script if you hit OK" with title scriptTitle default button 2 cancel button 1
			on error
				error number -128
			end try
			set contents of atag to (the clipboard as record)
			exit repeat
		else
			set i to i + 1
		end if
	end repeat
	
	
	if foundmissing = true and i = 9 then
		set my hasRecorded to true
		return 0
	else
		return 0
	end if
	
end if

if hasRecorded is true then
	
	
	set tagl to {{leadingnumber(0) & "<0/>"}, {leadingnumber(1) & "<1/>"}, {leadingnumber(2) & "<2/>"}, {leadingnumber(3) & "<3/>"}, {leadingnumber(4) & "<4/>"}, {leadingnumber(5) & "<5/>"}, {leadingnumber(6) & "<6/>"}, {leadingnumber(7) & "<7/>"}, {leadingnumber(8) & "<8/>"}, {leadingnumber(9) & "<9/>"}}
	
	
	set res to choosesomething("Choose tag to insert", tagl, item 1 of tagl)
	
	if res = false then return
	set the clipboard to item (((characters 1 thru 2 of (res as text)) as text as integer) + 1) of tags
	
	tell application "System Events" to tell application process "OmegaT"
		set its frontmost to true
		--		activate
		keystroke "v" using {command down}
	end tell
	
end if
on leadingnumber(n)
	return text 1 thru 3 of (n & "   " as text)
end leadingnumber

on choosesomething(aPrompt, listOfSomething, defaultSomething)
	local theChoice
	tell application "SystemUIServer"
		activate
		set theChoice to choose from list listOfSomething with prompt aPrompt with title my scriptTitle default items defaultSomething without multiple selections allowed and empty selection allowed
	end tell
	return theChoice
end choosesomething

If I’ve understood correctly, I started the upper script, the list appeared, I then tried to copy one of the tags (<0/>) and then clicked on the entry for 0 <0/>, but I got the following error:

error “Can’t make {"<", "0", "/", ">"} into type record.” number -1700 from {“<”, “0”, “/”, “>”} to record

I’ve been working more with my script to create a tag shell and I think it might be the quickest and easiest thing:

set tag_ to ("<t/>") as string
tell application "System Events"
	tell application "OmegaT" to activate
	tell process "OmegaT" to keystroke tag_
	key code 123 -- Left
	key code 123 -- Left
	delay 1
	key code 124 -- Right
	key code 124 -- Right
end tell

Your help has been fantastic and I really appreciate it.

My bigger problem now seems to be with getting an on-the-fly view of the output translation (which I’ve posed a new topic about here: http://macscripter.net/viewtopic.php?id=39197

I hope it’s ok to refer to another topic within this topic. If you don’t mind having a look at this, I would appreciate it. I’d like to be able to write a HowTo on this for the OmegaT site (and include you as the main source for solving the problem, if you don’t mind).

Regards,

Bowjest

Hello!

I have altered the coercion in the first script, and changed the copy operation to use “paste and retain style”, maybe that would help? please try that.

I thought other might need this as well, as it seems quirky to work with, that is why I asked you to post it, there is no need to reference me in this really. You are the instigator! :slight_smile:

I’m afraid it’s still the same as before. :frowning:

But not to worry. I think the empty shell insertion script is fast enough for what I need.

I just need to fix the file conversion problem I’m having.

Many, many thanks!

Bowjest

Ahh … but try now!

Now it doesn’t insert anything. :frowning:

Hello!

I am sorry to hear that, there are still some options left!

The first one is if you can see if you can find a menu choice (under the edit menu for paste and match style), this software being written in java, means that the keystrokes won’t necessarily conform 100%.

If we find that, then we are in luck!

If we aren’t, then we must find out if it is rtf you got there, and maybe we are in luck.

The second script I wrote, would “record” the attribute runs for bold for instance, but you will need it for bold and italic, and bold + italic, and plain,for it to make sense. I think that can be done, and it would be fun to manipulate the clipboard that way. Without you and me having to “record” :smiley:

But first of all, see if you can find “paste and match style” or similiar somewhere in the docs!
also check out if you can copy style (best solution) and paste style.

On mac os x those are cmd-opt C and cmd-opt V, it would nice if those worked, because then I can provide a fix, by copy the style either before or after the insertion point, before pasting back the tag.
:slight_smile:

Hello if copy and paste style doesn’t work, then I need to know your OmegaT font, size weight, anything you can find about it, that shows up in the font dialog, for starters.

I might give it a shot, to try to construct the styled text on the clipboard. But I have not OmegaT to play with, nor will I install it, due to Java Libraries. But if you suplly the information, then I can perhaps make a solution work with TextEdit, that will also work with OmegaT. :slight_smile:

I honestly don’t know.

In the Editor pane, text is displayed (as far as I can tell) in plain text. Only the tags are shown to indicate what formatting there might be (and this isn’t obvious from the tags themselves).

I’m sorry I can’t tell you more. I don’t have access to any of the programming specs.

Bowjest

Hello!

:slight_smile: Now this seems to get interesting! You have some text rendered as plain text in the editor pane, yet the text is rendered as RTF in your docx document when done.

We might be left in peril here! :frowning:

But please copy some text from the Editor pane, that you are sure containing bold (or italic formatting), then run the script below, and post the output from the log window of AppleScript editor, if you get any.

set x to the clipboard as record 
log scrap styles of x

Hi McUsr,

Thanks for taking such an active interest in this. It really is super of you to do this.

Here is the output I got after copying a sentence from a text and then running the script you included:

error “Can’t get scrap styles of {«class utf8»:"Atreus ist Marktführer beim Einsatz von Interim Managern in Deutschland und unterstützt seine Kunden, wenn der Bedarf an Umsetzungssicherheit und -geschwindigkeit groß ist.", «class ut16»:"Atreus ist Marktführer beim Einsatz von Interim Managern in Deutschland und unterstützt seine Kunden, wenn der Bedarf an Umsetzungssicherheit und -geschwindigkeit groß ist.", string:"Atreus ist Marktführer beim Einsatz von Interim Managern in Deutschland und unterstützt seine Kunden, wenn der Bedarf an Umsetzungssicherheit und -geschwindigkeit groß ist.", Unicode text:"Atreus ist Marktführer beim Einsatz von Interim Managern in Deutschland und unterstützt seine Kunden, wenn der Bedarf an Umsetzungssicherheit und -geschwindigkeit groß ist."}.” number -1728 from scrap styles of {«class utf8»:“Atreus ist Marktführer beim Einsatz von Interim Managern in Deutschland und unterstützt seine Kunden, wenn der Bedarf an Umsetzungssicherheit und -geschwindigkeit groß ist.”, «class ut16»:“Atreus ist Marktführer beim Einsatz von Interim Managern in Deutschland und unterstützt seine Kunden, wenn der Bedarf an Umsetzungssicherheit und -geschwindigkeit groß ist.”, string:“Atreus ist Marktführer beim Einsatz von Interim Managern in Deutschland und unterstützt seine Kunden, wenn der Bedarf an Umsetzungssicherheit und -geschwindigkeit groß ist.”, Unicode text:“Atreus ist Marktführer beim Einsatz von Interim Managern in Deutschland und unterstützt seine Kunden, wenn der Bedarf an Umsetzungssicherheit und -geschwindigkeit groß ist.”}

I can send you a screenshot of the Editor interface if that will help.

Regards,

Bowjest

Thank you, and that screenshot will not be necessary!

It was as I feared, there are two streams of characters here, one, which is what get what you type, since it manages to retain formatting information, like bold, and italcis. (If I have understood you right.) And the text shown again as pure ascii, presenting a view of the text in the editor window.

If there is any other place you process the text as RTF, then I’d like to know, as you did state that you wanted to keep the formatting, so, now I wonder, if there is any place you see this formatting.

But! :smiley: I think this will work pretty good, anyway, if I change the first script to type the text!

I’ll be back with a solution doing just that, somewhat later.

Hello!

Try this! :slight_smile:



property scriptTitle : "TagInserter"
set tagl to {{leadingnumber(0) & "<0/>"}, {leadingnumber(1) & "<1/>"}, {leadingnumber(2) & "<2/>"}, {leadingnumber(3) & "<3/>"}, {leadingnumber(4) & "<4/>"}, {leadingnumber(5) & "<5/>"}, {leadingnumber(6) & "<6/>"}, {leadingnumber(7) & "<7/>"}, {leadingnumber(8) & "<8/>"}, {leadingnumber(9) & "<9/>"}}


set res to choosesomething("Choose tag to insert", tagl, item 1 of tagl)

if res = false then return
set stringtoType to (characters 4 thru -1 of (res as text)) as text

tell application "OmegaT" to activate -- 

tell application "System Events"
	keystroke stringtoType
end tell

on leadingnumber(n)
	return text 1 thru 3 of (n & "   " as text)
end leadingnumber

on choosesomething(aPrompt, listOfSomething, defaultSomething)
	local theChoice
	tell application "SystemUIServer"
		activate
		set theChoice to choose from list listOfSomething with prompt aPrompt with title my scriptTitle default items defaultSomething without multiple selections allowed and empty selection allowed
	end tell
	return theChoice
end choosesomething


I copied the source text, ran your script and then pasted in the desired tag, but it’s still being seen as an extra symbol rather than being interpreted as a tag. :frowning:

Bowjest

Hello!

We are not out of options yet! :slight_smile: Because, it works if you type it?
Or is there anyway that works when you do it manually? I might try to use the keycodes, that may work, if it works when you type the tag.

Hello!

Please try this:



property scriptTitle : "TagInserter"
set charList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "<", "/", ">"}
set codeList to {29, 18, 19, 20, 21, 23, 22, 26, 28, 25, 50, 76, 100}
-- Edward Mendelson http://macscripter.net/viewtopic.php?id=39030
set tagl to {{leadingnumber(0) & "<0/>"}, {leadingnumber(1) & "<1/>"}, {leadingnumber(2) & "<2/>"}, {leadingnumber(3) & "<3/>"}, {leadingnumber(4) & "<4/>"}, {leadingnumber(5) & "<5/>"}, {leadingnumber(6) & "<6/>"}, {leadingnumber(7) & "<7/>"}, {leadingnumber(8) & "<8/>"}, {leadingnumber(9) & "<9/>"}}


set res to choosesomething("Choose tag to insert", tagl, item 1 of tagl)

if res = false then return
set stringtoType to (characters 4 thru -1 of (res as text)) as text
set codestoHammer to {}
repeat with i in every character in stringtoType
	set a to offset of i in charList as text
	set end of codestoHammer to item a of codeList
end repeat

tell application "OmegaT" to activate -- 

tell application "System Events"
	set frontmost to true
	repeat with a in codestoHammer
		if (contents of a) > 50 then
			key code ((contents of a) - 50) using {shift down}
		else
			key code contents of a
		end if
	end repeat
end tell

on leadingnumber(n)
	return text 1 thru 3 of (n & "   " as text)
end leadingnumber

on choosesomething(aPrompt, listOfSomething, defaultSomething)
	local theChoice
	tell application "SystemUIServer"
		activate
		set theChoice to choose from list listOfSomething with prompt aPrompt with title my scriptTitle default items defaultSomething without multiple selections allowed and empty selection allowed
	end tell
	return theChoice
end choosesomething