Applescript to modify xml files in Xcode.

Hi All,

Is it possible to ‘script’ Xcode?

Ie. If a have a bunch of files that I want to modify the text of in Xcode can it be scripted to do this? I have started with the simple:


tell application "Finder"
	set xmlfile to choose file "where is the exported xml file?"
	end tell

tell application "Xcode"
	activate
	open xmlfile
	end tell

What is the command syntax to copy the contents of the front window?

I’m trying


set xmldoc to text document 1
tell xmldoc
select all <- doesn't work
select all text <- doesn't work
select all paragraphs <- doesn't work
end tell

also the copy command once the text is selected. (I can edit the text in an external text editor) or if it’s easy to find/change in Xcode (with Applescript) please advise.

Thanks in Advance.

Matt

Hi,

first of all XML is plain text. You can open and edit it in any text editor or even in AppleScript using text item delimiters. Xcode is not needed.
Anyway Xcode is pretty well scriptable, to select the entire text of an Xcode document use this


tell application "Xcode"
	set xmldoc to text document 1
	tell xmldoc
		set textlength to length of (get its text)
		set selected character range to {1, textlength}
	end tell
end tell

PS: This is one of the rare cases where its is mandatory.

If you want to copy the contents of the Xml file open inn Xcode you may use :

tell application "Xcode"
	
	set theText to text of text document 1
	
end tell
set the clipboard to theText

As Stefan wrote, every text editor may treat Xml files so my best choice would be TextWrangler (free) or BBEdit ($49.99).
Both are heavily scriptable tools.

Yvan KOENIG (VALLAURIS, France) mardi 20 janvier 2015 15:41:29

Large dictionaries, and a complex object model, may feel a bit intimidating for starters, but both of them supports the recording of AppleScript Editor pretty well, so there is nothing holding you back if you wonder how to script a particular workflow. There are also great resources on the net.

But, sometimes one have to pick the battles, if you live in Xcode, then I see no reason why you shouldn’t learn to script it, and spend your efforts there.

@StefanK

That was the second time in a short row where you had to use its ;).

Edit

On more thing: XML Documents is only valid with utf-8 encoding, so you have to ensure that it is saved with the correct encoding. This is easy to see in TextWrangler and BBEdit in the status bar, in Xcode and TextEdit you see it in their preferences (default in Xcode, probably so in TextEdit).

Hi All,

Thanks for the input as always.

I had been trying to modify text in InDesign by renaming the xml file, then placing into Indesign. The character formatting went haywire! I noted that in Xcode the source xml was correct, and thought I’d just copy and past from there - hence the Xcode query, which I was sure they’d be a simple answer for (Thanks Stefan K & Yvan Koenig).

McUsrII noted encoding in the footnote (Thanks) and sure enough with a tweak to the Indesign text import preferences, I’ve got the text the way I need.

tell text import preferences
		set character set to UTF8
		set platform to macintosh
	end tell

Yes, I could edit the text directly in AS (or a text editor), Indesign is just a more familiar scripting application for me.

Warm Regards,

Matt.