File Merging Tool With AppleScript Fails

I’m trying to write a script that will allow me to merge some specialists data files using a tool called TMXMerger.

Here is the script I have so far:

set theFolder to choose folder with prompt "Customer documents :" default location "/Users/bowjest/Translations/Projects"

tell application "Finder"
	set theItems to items of folder theFolder
	set theFolderPOSIX to POSIX path of theFolder
	
	set theNames to {}
	repeat with anItem in theItems
		set end of theNames to name of anItem
	end repeat
end tell

choose from list theNames with prompt "Pick the items you want to merge from:" & return & theFolder OK button name "Merge" cancel button name "Quit" with multiple selections allowed
tell result
	if it is false then error number -128 -- cancel
	set theChoices to it
end tell

if (count of theChoices) is greater than or equal to 1 then
	repeat with aChoice in theChoices
		set thisItem to theFolder & aChoice
		do shell script "cd " & quoted form of theFolderPOSIX & " ; java -jar /Applications/TMXMerger/TMXMerger-1.0.jar " & theChoices & " " & Output & " "
	end repeat
end if

When I try to run it, however, I get the error “The variable Output is not defined”.

The README file for TMXMerger states:

"TMXMerger-1.0.jar must be executed on the command line.

The command to execute TMXMerger-1.0.jar has the following form:

java -jar TMXMerger-1.0.jar [source=LL(-CC)] [ …]

Where: is the name of the first TMX file, the name of the second TMX file, etc. is the name of the merged file."

This is probably a big schoolboy error, but as I’m not much of an AppleScripter, I’m stumped as to how to get around this.

Can anyone advise where I’m going wrong?

Many thanks,

Bowjest

Well…
The variable Output is not defined, just like the error message says:

do shell script "cd " & quoted form of theFolderPOSIX & " ; java -jar /Applications/TMXMerger/TMXMerger-1.0.jar " & theChoices & " " & Output & " "

You have to set Outpout to some value, like a file name, before using it.

Hello!

The problem is that the variable Output isn’t defined. :slight_smile:

Try this:

set theFolder to choose folder with prompt "Customer documents :" default location "/Users/bowjest/Translations/Projects"

tell application "Finder"
	set theItems to items of folder theFolder
	set theFolderPOSIX to POSIX path of theFolder
	
	set theNames to {}
	repeat with anItem in theItems
		set end of theNames to name of anItem
	end repeat
end tell

choose from list theNames with prompt "Pick the items you want to merge from:" & return & theFolder OK button name "Merge" cancel button name "Quit" with multiple selections allowed
tell result
	if it is false then error number -128 -- cancel
	set theChoices to it
end tell

set Output to choose file name with prompt "Choose the file you would like to put the result in" default location "/Users/bowjest/Translations/Projects"
set Output to quoted form of POSIX path of Output
if (count of theChoices) is greater than or equal to 1 then
	repeat with aChoice in theChoices
		set thisItem to theFolder & aChoice
		do shell script "cd " & quoted form of theFolderPOSIX & " ; java -jar /Applications/TMXMerger/TMXMerger-1.0.jar " & theChoices & " " & Output & " "
	end repeat
end if

Sorry, everyone, I wasn’t very clear in what I was trying to say.

I realise that the variable Output isn’t defined, but am not sure how to define it considering that TMXMerge expects to produce an output on its own (unless I have misunderstood how it works. I haven’t used it in a couple of years).

Thanks, McUsr, I’ll give your suggestion a try in the morning.

Thanks to all,

Bowjest

It does “produce output on its own”. You just have to tell it where to put it.

Stiil, there are other things wrong with the script as it stands.
theChoices, in the shell command, should be a list in a form that the shell can understand. That means (quoted) POSIX paths, separated by spaces.
That’s what the repeat loop should do, but doesn’t.

The shell command should be executed after the loop has built the desired list.

To summarise: TMXMerge takes a list of (quoted) POSIX file paths. The last one will receive the produced output.

Thanks, alastor933.

That will explain why when I run the script and select where to save the results (Desktop), it starts to run, but doesn’t go anywhere.

I’m not very good at this POSIX quoting stuff as I honestly don’t understand what it’s all about (I try to come up with a script about once ever 6-8 months on average).

The info displayed in the Results pane in my AppleScript editor shows the following when I run the script and select the files I want to process:

Reading TMX file /Users/bowjest/Translations/Projects/auto/§1204"

The file names have spaces in them, so it appears that the system is failing to read them correctly because of this.

I tried the following:

choose from list theNames with prompt "Pick the items you want to merge from:" & return & theFolder OK button name "Merge" cancel button name "Quit" with multiple selections allowed
tell result
	if it is false then error number -128 -- cancel
	set theChoices to it
	set theChoicesPOSIX to quoted form of POSIX path of theChoices
end tell

But when I ran the script it threw up the following message:

"“Can’t get POSIX path of {"§1204 file name 01.tmx","§1204 file name 02.tmx" etc.”

Can anyone advise on how I can ensure all the files selected will have their paths fully quoted when the script is run?

Thanks,

Bowjest

Hello see if this works better!


set theFolder to choose folder with prompt "Customer documents :" default location "/Users/bowjest/Translations/Projects"

tell application "Finder"
	set theItems to items of folder theFolder
	set theFolderPOSIX to POSIX path of theFolder
	
	set theNames to {}
	repeat with anItem in theItems
		set end of theNames to name of anItem
	end repeat
end tell

choose from list theNames with prompt "Pick the items you want to merge from:" & return & theFolder OK button name "Merge" cancel button name "Quit" with multiple selections allowed
tell result
	if it is false then error number -128 -- cancel
	set theChoices to it
end tell

set Output to choose file name with prompt "Choose the file you would like to put the result in" default location "/Users/bowjest/Translations/Projects"
set Output to quoted form of POSIX path of Output
set flist to ""

repeat with aname in theChoices	
	set flist to flist & theFolderPOSIX & aname & " "
end repeat


if (count of theChoices) is greater than or equal to 1 then	
	do shell script "cd " & quoted form of theFolderPOSIX & " ; java -jar /Applications/TMXMerger/TMXMerger-1.0.jar " & quoted form of (flist & Output)
end if


Thanks, McUsr,

I just tried that and although it completed without any errors, it also doesn’t appear to have done anything.

No file has been saved to my Desktop and the Results pane just has “” in it.

I’m afraid I don’t know what this means or how to approach what else to do.

Regards,

Bowjest

I may have assumed too much. Bowjest’s quote of the TMXMerge Readme actually says something else.
It mentions a ‘source’ item. Might that be the container of the files to be merged?

try changing the line with output file first to


set Output to quoted form of  POSIX path of Output as text

If that doesn’t work, ( it really shouldn’t), then It may very well happen that the file merge program, sends its output to stdout.

So please try this in the do shell script line


set myOut to  do shell script "cd " & quoted form of theFolderPOSIX & " ; java -jar /Applications/TMXMerger/TMXMerger-1.0.jar " & quoted form of (flist & Output)

log myOut

See if you get any output in the log pane, then if you do, then change the line specifying the output file to:


set Output to quoted form of (">" & POSIX path of Output as text)

Then the output should be piped into that file. I hope it works

Hi McUsr,

I tried each of your suggestions in turn and although no errors were thrown, the script still doesn’t produce an output file of any sort. Hmmmm.

Not sure what to do, really. I’m trying to merge all my tmx files into one master file, but if I have to type out each file name on the command line (and I have a couple of thousand tmx files) I can’t see it being worth the effort.

Am happy to try any suggestions and appreciate your and everyone’s input and help.

Regards,

Bowjest

Hello!

I read this.

Try calling your output file output.tmx

I am also unsure if you really can have a filelist bigger than two files, from some of the stuff I read, and, maybe the files need to be specified, in a certain order, trunk version of the file first, then the branches.

This seems logical to me, as the changes, must be applied sequentially, at least to be meaningful, when you use regular diff tools, like diff and diff3.

And also, try to see, or read up to find out if there is a log generated somewhere, that may be helpful!

Hi McUsr,

Thanks for that. TMX files don’t work like code in a project, so there is not order or check in/out. The order in which they are combined doesn’t really matter as they are scanned from top to bottom by most CAT tools.

I did try calling the output file output.tmx, but that didn’t get me any further, sadly. :frowning:

I just tried to run the script again and this time I got:

Cannot quote POSIX path of (then all the files selected are listed).

The part highlight in the script is this:

set theChoicesPOSIX to quoted form of POSIX path of theChoices

I also don’t think TMXMerger cares about the number of files as I’ve done quite a few from the cli in the past, it’s just tedious if you need to combine more than just a few.

Any suggestions very much appreciated.

Hello!
how do you specify the output file, when you are working from the cli?

if you could copy in a commandline, you have used that worked, then we may do some progress! :slight_smile:

As for your code, I had quite another way to do it:


if (count of theChoices) is greater than or equal to 1 then    
   do shell script "cd " & quoted form of theFolderPOSIX & " ; java -jar /Applications/TMXMerger/TMXMerger-1.0.jar " & quoted form of (flist & Output)
end if

You will need spaces between the filenames, not the list item separator then you’ll at least have to set Applescript’s text item delimiters to a space, before you coerce the list to text and be sure to set back Applescript’s text item delimiters afterwards to their previous value.

You won’t believe this, but just for the hell of it, I cd’d into a directory with some tmx files and ran the following:

java -jar TMXMerger-1.0.jar * output.tmx

and it bloody well worked. :slight_smile:

I don’t know whether to laugh or cry.

So here is the script I just knocked up that seems to do the job:

set default_folder_Location to (path to home folder as text) & "Translations:Projects:"
set tmxFolder to (choose folder with prompt "TMX Folder :" default location alias default_folder_Location) as text
set tmxFolderPOSIX to POSIX path of tmxFolder -- POSIX path representation of project_location
set Output to choose file name with prompt "Choose output file name" default location "/Users/bowjest/Translations/Projects"
set Output to quoted form of POSIX path of Output

do shell script "cd " & quoted form of tmxFolderPOSIX & " ; java -jar /Applications/TMXMerger/TMXMerger-1.0.jar * " & " " & Output & " "

display alert "TMX files merged"

Oh, well, so much for RTFM.

I’m just glad it seems to work. I’ll sanity check things in the morning.

Thanks to everyone (especially McUsr) for their help.

Bowjest