Can't run Java tool with AppleScript

I’m trying to automate the conversion of a file into a different format using AppleScript and a tool from the Okapi Framework.

If I run the following from the command line it works every time:

java -jar /Applications/okapi-apps_cocoa-macosx-x86_64_0.16/lib/tikal.jar -x -sl DE -tl EN *.docx

If, however, I try to select the directory where I have a file and run this using AppleScript it fails:

set project_location to choose folder with prompt "Customer documents :" default location "/Users/bowjest/Desktop"
do shell script "java -jar /Applications/okapi-apps_cocoa-macosx-x86_64_0.16/lib/tikal.jar -x -sl DE -tl EN *.docx"

The error I get is:

error "-------------------------------------------------------------------------------
Okapi Tikal - Localization Toolset
Version: 2.0.16

Extraction
Source language: de
Target language: en
Default input encoding: MacRoman
Filter configuration: okf_openxml
Output: /*.docx.xlf

– Input: /*.docx
ERROR: Error opening zipped input file.
You can use the -trace option for more details." number 1

I’m assuming the problem is using “*.docx” when I call the java tool. How should I designate this to include all .docx files in the directory if * is not the right variable to use?

Thanks,

Bowjest

Ok, I’ve tried this:

set default_folder_Location to (path to home folder as text) & "Folders"
set folder_location to POSIX path of (choose folder with prompt "Folder :" default location alias default_folder_Location)
do shell script "cd $folder_location && java -jar /Applications/okapi-apps_cocoa-macosx-x86_64_0.16/lib/tikal.jar -x -sl DE -tl EN *.pptx"

But sadly this also fails:

error "-------------------------------------------------------------------------------
Okapi Tikal - Localization Toolset
Version: 2.0.16

Extraction
Source language: de
Target language: en
Default input encoding: MacRoman
Filter configuration: okf_openxml
Output: /Users/bowjest/*.pptx.xlf

– Input: /Users/bowjest/*.pptx
ERROR: Error opening zipped input file.
You can use the -trace option for more details." number 1

It doesn’t seem to actually change directory.

There must be a simple way to change directory to a folder specified in a variable and then run a script (or in this case, a Java app), but I just can’t figure out what it is.

Anyone know where I’m going wrong with this?

Thanks,

Bowjest

I don’t know anything about Java so don’t get your hopes up:


set folder_location to quoted form of POSIX path of (choose folder with prompt "Folder :" default location alias default_folder_Location)
do shell script "cd " & folder_location & " ; java -jar /Applications/okapi-apps_cocoa-macosx-x86_64_0.16/lib/tikal.jar -x -sl DE -tl EN *.pptx"

Thanks, adayzdone!

That’s taken care of it.

Much appreciated. This will save me a lot of wasted time in future.

Bowjest

Oops! I’ve just realised I’ve made a fundamental error: the Tikal tool can convert multiple file types, not just .pptx or .docx.

Can this be easily modified so that it will run Tikal against any file in the selected directory?

Sorry, I completely forgot about this aspect of the tool as I’m still learning to use it.

UPDATE: I’ve fudged this for the time being by putting in a “try” statement and then adding *.docx at the end of the file type list.

Not ideal, but it seems to work. If there’s a cleaner solution, please do let me know.

Bowjest

Hi adayzdone,

I got a mail saying you had supplied an update, but when I clicked on the link I got the following:

“Bad request. The link you followed is incorrect or outdated.”

Not sure what happened there.

There’s a second element to this script that I’d like to implement, but not sure how practical that will be. I’ll explain:

After using the XLIFF file created using the first half of this script (thanks again to adayzdone), I can translate the data contained therein and then export the final translation, which creates a new version of the XLIFF file, but in the target language.

To check that this new XLIFF file is correct and can create a viable, new language version of the original doc, I use Tikal to merge it with the format data from the original document. This is done by taking the new XLIFF file out of the target directory of the project and putting it into the source directory where the first XLIFF was created (which means I either have to delete or rename the original XLIFF file) and then running the merge function in Tikal.

I can do this move and replace step manually and then use a modified version of the script discussed here to merge things, but it would be helpful if I could automate this move and replace function. In essence, here is what I would need to do:

  1. CD into the source directory and either delete or rename the initial .xlf file

I tried doing this by naming the Project directory (one level above the source and target directories) as the starting point and then working from there:

set default_folder_Location to (path to home folder as text) & "Documents"
	set project_location to quoted form of POSIX path of (choose folder with prompt "Folder :" default location alias default_folder_Location)
	try
		do shell script "cd " & project_location/source & " ; mv *.xlf *.xlf.src"
	end try

This has failed - most likely due to my lack of understanding of how variables are handled between AS and shell scripts.

  1. Move all .xlf files from the target directory to the source directory and merge them with the formatting data from their original files:
do shell script "cd " & project_location/target & " ; mv *.xlf" & project_location/source" ; java -jar /Applications/okapi-apps_cocoa-macosx-x86_64_0.16/lib/tikal.jar -m -sl DE -tl EN *.xlf"

Again, this fails. :frowning:

If it did work, it would produce out.file_format files for each .xlf file in the directory (i.e. target_file.out.docx or target_file.out.pptx, etc.)

  1. Move all .xlf and .out.* files back to the target directory
do shell script "cd " & project_location/source & " ; mv *.xlf, *.out.*  project_location/target"

I hope this makes sense even if so far I’ve not managed to get much of it working.

Thanks for any advice on getting this right. It would really be a time saver for me.

Bowjest

You cannot mix variables with literal strings, in almost all programming/scripting languages literal strings must be quoted. To get a quoted form of the “source” folder of the project_location folder use this


set default_folder_Location to (path to documents folder)
set project_location to quoted form of (POSIX path of (choose folder with prompt "Folder :" default location default_folder_Location) & "source")
try
	do shell script "cd " & project_location & " ; mv *.xlf *.xlf.src"
end try


Thanks, Stefan.

That seems to work, but I have two questions:

  1. When using this, the existing .xlf file in the source directory is changed from TEST.xlf to *.xlf.src and my intention was to rename it to TEST.xlf.src. The idea here, of course, was to rename any and all .xlf files from what they currently are to the same thing, but with the addition of .src on the end. Again, a problem on my part with the syntax. Do I need some sort of “for” statement here to ensure these get renamed correctly?

  2. As I will need to work between the source and target directories, how would I need to define both of these for use throughout the rest of the script?

I tried the following, but it failed to even complete the first part of the renaming phase (i.e. mv *.xlf *.xlf.src):

set default_folder_Location to (path to home folder as text) & "01 Translations:03 Projects"
set project_location to quoted form of POSIX path of (choose folder with prompt "Folder :" default location alias default_folder_Location)
set source_location to quoted form of POSIX path of default_folder_Location & "source"
set target_location to quoted form of POSIX path of default_folder_Location & "target"
try
	do shell script "cd " & source_location & " ; mv *.xlf *.xlf.src"
	do shell script "cd" & target_location & " ; mv *.xlf source_location
	do shell script "cd" & source_location & " ; java -jar /Applications/okapi-apps_cocoa-macosx-x86_64_0.16/lib/tikal.jar -m -sl DE -tl EN *.xlf"
	do shell script "cd" & source_location & " ; mv *.out.* target_location"
	do shell script "cd" & source_location & " ; mv *.xlf target_location"
end try

display alert "XLIFF file merged"

Again, the idea being that:

  1. The original .xlf file or files are renames from orig_xlf_file.xlf to orig_xlf_file.xlf.src
  2. The .xlf file(s) created in the target directory are moved to the source directory
  3. The merge function is run and the resulting merged .xlf file(s) and .out. files (e.g. file.out.docx or file.out.pptx) are moved back to the target directory.

Hopefully I’m not too far off, but I just can’t figure out my syntax errors (but I’m the first to admit I don’t really understand how variables are handled in AS).

Thanks for your help.

Bowjest

For renaming and moving multiple files you need a repeat loop, mv can’t do that in one line
Try this, I haven’t tested it because I don’t have your environment

In most of the lines the script uses absolute paths instead of choosing directories with cd


set default_folder_Location to (path to home folder as text) & "01 Translations:03 Projects:"
set project_location to (choose folder with prompt "Folder :" default location alias default_folder_Location) as text
set project_location_POSIX to POSIX path of project_location -- POSIX path representation of project_location
set source_location to project_location & "source:"
set source_location_POSIX to POSIX path of source_location -- POSIX path representation of source_location
set target_location to project_location & "target:"
set target_location_POSIX to POSIX path of target_location -- POSIX path representation of target_location

tell application "Finder" to set xlfFiles to files of folder source_location whose name extension is "xlf"
repeat with aFile in xlfFiles
	set aFilePOSIX to POSIX path of (aFile as text)
	do shell script "mv " & quoted form of aFilePOSIX & space & quoted form of (aFilePOSIX & ".src")
end repeat
do shell script "mv" & quoted form of target_location_POSIX & "*.xlf" & space & quoted form of source_location_POSIX
do shell script "cd " & quoted form of source_location_POSIX & " ; java -jar /Applications/okapi-apps_cocoa-macosx-x86_64_0.16/lib/tikal.jar -m -sl DE -tl EN *.xlf"
tell application "Finder" to move (every file of folder source_location whose name contains ".out." or name extension is "xlf") to folder target_location

display alert "XLIFF file merged"

Thanks, Stefan.

I’m not sure which directory I’m supposed to select when I run this (project name, source or target), but find I get an error when I try all three:

  1. Choosing the top level TEST project name (one level above source and target):

error “sh: mv/Users/bowjest/01 Translations/03 Projects/TEST/target/*.xlf: No such file or directory” number 127

The line in the code that is highlighted in blue is:

do shell script “mv” & quoted form of target_location_POSIX & “*.xlf” & space & quoted form of source_location_POSIX

There is, however, a test.xlf file in the source directory.

  1. Choosing the source directory (one level below TEST):

error “Finder got an error: Can’t get folder "Macintosh HD:Users:bowjest:01 Translations:03 Projects:TEST:source:source:".” number -1728 from folder “Macintosh HD:Users:bowjest:01 Translations:03 Projects:TEST:source:source:”

The line in the code that is highlighted in blue is:

tell application “Finder” to set xlfFiles to files of folder source_location whose name extension is “xlf”

  1. Choosing the target directory (one level below TEST):

error “Finder got an error: Can’t get folder "Macintosh HD:Users:bowjest:01 Translations:03 Projects:TEST:target:source:".” number -1728 from folder “Macintosh HD:Users:bowjest:01 Translations:03 Projects:TEST:target:source:”

The line in the code that is highlighted in blue is:

tell application “Finder” to set xlfFiles to files of folder source_location whose name extension is “xlf”

Does that ring any bells?

Thanks,

Bowjest

You have to choose the directory which contains the source and target folders (TEST)

In this line there must be a space character after mv


do shell script "mv " & quoted form of target_location_POSIX & "*.xlf" & space & quoted form of source_location_POSIX

Hi Stefan,

There is a space between mv and the rest of that line.

When I run the script and select TEST as the directory, I still get this error:

error “sh: mv/Users/bowjest/01 Translations/03 Projects/TEST/target/*.xlf: No such file or directory” number 127

The line in the code that is highlighted in blue is:

do shell script “mv” & quoted form of target_location_POSIX & “*.xlf” & space & quoted form of source_location_POSIX

And just to confirm, there is a .xlf file in the target directory.

Thanks,

Bowjest

As you can see in the error message, there is no space between mv and /Users

But there is a space in the actual code:

do shell script "mv" & quoted form of target_location_POSIX & "*.xlf" & space & quoted form of source_location_POSIX

I’m afraid I don’t know how to force a space into how AS interprets the code.

Bowjest

Please look carefully, do you see the difference between

do shell script "mv"

and

do shell script "mv "

The space characters in the source code don’t matter, only the literal strings and the contents of the variables are relevant

You can also write


do shell script "mv" & space & quoted form of target_location_POSIX & "*.xlf" & space & quoted form of source_location_POSIX

Sorry, I’m blind. I see it now.

:lol:

One second and I’ll try again.

Ok, it’s almost perfect.

The only thing it’s not doing is:

  1. It’s not renaming the original .xlf file in the source directory to .xlf.src - this is being overwritten when the target_file.xlf file is moved over

  2. It’s not moving the target_file.xlf back to the target directory - this is staying in the source directory

I noticed that the line of code that mentioned the .out. and xlf extension was worded as follows:

tell application "Finder" to move (every file of folder source_location whose name contains ".out." or name extension is "xlf") to folder target_location

I noticed there wasn’t a dot (.) before xlf, so I added this, but it made no difference.

I then tried changing the wording slightly to this:

tell application "Finder" to move (every file of folder source_location whose name contains ".out." or whose name contains "xlf") to folder target_location

but I guess you can’t have two instances of “whose name contains” in one line.

Other than that, it seems to work great. :slight_smile:

Any advice on the above points or have you had enough fun for one Saturday? :slight_smile:

Thanks,

Bowjest

I’m an idiot:

The simple answer to moving the .xlf file back to the target directory is:

tell application "Finder" to move (every file of folder source_location whose name contains ".out.") to folder target_location
tell application "Finder" to move (every file of folder source_location whose name contains "xlf") to folder target_location

Just repeat the line with the two separate entries. Works now! :slight_smile:

If you can advise on renaming the source .xlf file to .xlf.src (or .src.xlf) that would complete the picture!

Bowjest

The renaming is done on this part of the script


tell application "Finder" to set xlfFiles to files of folder source_location whose name extension is "xlf"
repeat with aFile in xlfFiles
   set aFilePOSIX to POSIX path of (aFile as text)
   do shell script "mv " & quoted form of aFilePOSIX & space & quoted form of (aFilePOSIX & ".src")
end repeat

I suspect that the name extension xlf is not a real name extension,
that means the Finder doesn’t treat xlf as name extension

Ok, I see what you mean.

I’ve gotten around that by changing the code slightly:

tell application "Finder" to set xlfFiles to files of folder source_location whose name contains "xlf"

This, however, now means that the .xlf.src file gets moved back to the target directory with the other files.

Is there a way of saying something like this:

tell application "Finder" to move (every file of folder source_location whose name contains "xlf" and does not contain "xlf.src") to folder target_location

Bowjest