Shortcut to translate QuarkX selection into Applescript

Curious if anyone knows a trick/shortcut to translate a selection of text in QX to its equivalent in Applescript syntax?

In other words, I want to get the syntax that would refer to

http://dl.getdropbox.com/u/499885/Picture%201.jpg

which is:
“Return” & space & return & “Before” & space & return & “Taxes” & tab

without manually having to key it. Seems like there would be a way to get A/S to take the current selection in Q and return the code, maybe to the clipboard.

Lazy? Or efficient?
Yes!

I’m not sure what you’re asking. I think you want a string representation of the applescript commands so you can paste it into some other application??? I do not have QX but I made this work for TextEdit. You just have to adapt it for QX. It’s really just multiple find/replace commands. It worked for your example but you’ll have to check it for other cases.

-- put the selected text on the cliboard so we can get it into applescript
tell application "TextEdit" to activate
delay 0.75
tell application "System Events" to tell process "TextEdit"
	keystroke "c" using command down
end tell
set selectedText to the clipboard

-- first you have to add quote marks to all of the words
set theWords to words of selectedText
repeat with i from 1 to count of theWords
	set selectedText to findAndReplace(selectedText, item i of theWords, "\"" & item i of theWords & "\"")
end repeat

-- next we replace space, return, and tab with their equivalent words surrounded by "&" characters
set selectedText to findAndReplace(selectedText, space, "& space &")
set selectedText to findAndReplace(selectedText, return, "& return &")
set selectedText to findAndReplace(selectedText, "	", "& tab &")

-- now we add spaces between the quoted words and the "&" characters
set selectedText to findAndReplace(selectedText, "\"&", "\" &")
set selectedText to findAndReplace(selectedText, "&\"", "& \"")

-- next we take care of multiple && instances
repeat while selectedText contains "&&"
	set selectedText to findAndReplace(selectedText, "&&", "&")
end repeat

-- next we take care of multiple quote mark instances
repeat while selectedText contains "\"\""
	set selectedText to findAndReplace(selectedText, "\"\"", "\"")
end repeat

-- put quoted words next to each other back together
repeat while selectedText contains "\" & space & \""
	set selectedText to findAndReplace(selectedText, "\" & space & \"", space)
end repeat

-- finally we clean up the beginning and ending of the final selectedText string
if selectedText begins with "& " then set selectedText to text 3 thru -1 of selectedText
if selectedText ends with " &" then set selectedText to text 1 thru -3 of selectedText

-- put the result back onto the clipboard
set the clipboard to selectedText



(*================ SUBROUTINES ==================*)
on findAndReplace(thetext, findText, replaceText)
	set {tids, text item delimiters} to {text item delimiters, findText}
	set a to text items of thetext
	set text item delimiters to replaceText
	set b to a as text
	set text item delimiters to tids
	return b
end findAndReplace

I think you’ve coded what I want, but my goal is not so I can paste into some other app, its so I can paste it into Script Editor.

I’ll give it a whirl tomorrow when I’m at work.

Thanks for this.

Hello.

@Hank: That is a real nice time saver, thanks! :slight_smile:

Best Regards

McUsr

I was playing with the script I posted and I wanted it to work on the following selected text in TextEdit. Note that this is just gibberish text as I was only trying to give it a tough example.

My previous script failed miserably on that text. It seems as soon as I added punctuation the script failed. Also, if I had a word like “A” and another word like “bake” which contained an “a” that the script would also fail. In that case “bake” would turn into “b"A"ke”… not too good. So I had to rethink how to handle these cases. It came down to how I was using “words” to quote the words of the text. “Words” doesn’t account for punctuation so punctuation was being left out of the quotes. To handle the second problem I had to add “considering case”.

So the solution was to just quote every character and then iterate through the quoted text and remove any quotes that didn’t belong. As such I came up with this and it seems to work well.

property standardReplaceCharacters : {space, return, character id 9, character id 10}
property wordsForStandardReplaceCharacters : {"space", "return", "tab", "return"}

-- put the selected text on the clipboard so we can get it into applescript
tell application "TextEdit" to activate
delay 0.5
tell application "System Events" to tell process "TextEdit"
	keystroke "c" using command down
end tell
delay 0.5
set selectedText to the clipboard

-- quote all the characters
set myChars to characters of selectedText
set didChar to {}
repeat with i from 1 to count of myChars
	set thisChar to item i of myChars
	considering case
		if thisChar is not in standardReplaceCharacters and thisChar is not in didChar then
			set end of didChar to thisChar
			set selectedText to findAndReplace(selectedText, thisChar, "\"" & thisChar & "\"")
		end if
	end considering
end repeat

-- clean up the quotes
set selectedText to cleanUpQuotes(selectedText)

-- next we replace space, return, and tab with their equivalent words surrounded by "&" characters
repeat with i from 1 to count of standardReplaceCharacters
	set findChar to item i of standardReplaceCharacters
	set replaceChar to item i of wordsForStandardReplaceCharacters
	set selectedText to findAndReplace(selectedText, findChar, "& " & replaceChar & " &")
end repeat

-- now we add spaces between the quoted words and the "&" characters
set selectedText to findAndReplace(selectedText, "\"&", "\" &")
set selectedText to findAndReplace(selectedText, "&\"", "& \"")

-- next we take care of multiple && instances
repeat while selectedText contains "&&"
	set selectedText to findAndReplace(selectedText, "&&", "&")
end repeat

-- put quoted words next to each other back together
repeat while selectedText contains "\" & space & \""
	set selectedText to findAndReplace(selectedText, "\" & space & \"", space)
end repeat

-- finally we clean up the beginning and ending of the final selectedText string
if selectedText begins with "& " then set selectedText to text 3 thru -1 of selectedText
if selectedText ends with " &" then set selectedText to text 1 thru -3 of selectedText

-- put the result back onto the clipboard
set the clipboard to selectedText



(*================ SUBROUTINES ==================*)
on cleanUpQuotes(thetext)
	set textCount to count of thetext
	if textCount is 1 then return thetext
	
	-- determine which quote characters need removal
	set quotesToRemove to {}
	repeat with i from 2 to (textCount - 1)
		if character i of thetext is "\"" then
			if character (i + 1) of thetext is not in standardReplaceCharacters and character (i - 1) of thetext is not in standardReplaceCharacters then
				set end of quotesToRemove to i
			end if
		end if
	end repeat
	
	-- iterate through the quotesToRemove list in reverse order so we maintain the proper character positions
	if quotesToRemove is not {} then
		repeat with i from (count of quotesToRemove) to 1 by -1
			set thisNumber to item i of quotesToRemove
			set thetext to text 1 thru (thisNumber - 1) of thetext & text (thisNumber + 1) thru -1 of thetext
		end repeat
	end if
	return thetext
end cleanUpQuotes

on findAndReplace(thetext, findText, replaceText)
	set {tids, text item delimiters} to {text item delimiters, findText}
	considering case
		set a to text items of thetext
	end considering
	set text item delimiters to replaceText
	set b to a as text
	set text item delimiters to tids
	return b
end findAndReplace

So all-in-all it probably could use a little more refinement, but it’s working pretty well. I started helping with this question just for fun but I think now I’ll get some use out of the script too.

I’m just getting back to this, and I had to substitute

property standardReplaceCharacters : {space, return, (ASCII character 9), (ASCII character 10)}

for

property standardReplaceCharacters : {space, return, character id 9, character id 10}

but its fantastic. This could (will) help me tremendously…

thank.
you.

Hello.

This works very well. :slight_smile:

It is sure nicer to edit the text as you want it in a tex editor and than have an AppleScript to convert it to AppleScript!

Best Regards

McUsr

Glad you both like it. I think it’s working well and I’ve used it already myself. AutoFetishist, I guess you must be using 10.4 because I believe that “character id” was implemented in 10.5. Anyway, good luck.

Hello.

Nigel Garvey was so kind as to show me this way to circumvent this particular problem.



set lf to (run script "\"\\n\" as Unicode text")
set tab to (run script "\"\\t\" as Unicode text")

Best Regards
McUsr

You are correct Regulus. Still in 10.4…

–sigh

Minor bug;

I seem, in the process of copying from QXP to Script Editor, to be losing soft returns. Having them converted to hard returns.

Is there an Applescript constant for ASCII char. 7? I obviously don’t want to have soft returns dropped, as this will disable string searches.

I guess I’m curious where / what is causing the translation error.