Can't copy file to directory using AppleScript

Hello to all,

I’ve put together a script for creating new projects for my work.

I’ve wanted to extend this by adding a bit to copy over a default .xlf file so that I can instantly work from a pre-configured project template (the default .xlf file), but so far I can’t seem to get this to work.

Here is what I have:

-- ------------------------------------------------------------
-- A script to create projects for CafeTran
-- ------------------------------------------------------------

-- ------------------------------------------------------------
-- Sets selection options
-- ------------------------------------------------------------
set option to {"Full project"}
#set option to {"Full project", "Email project", "CafeTran project", "Create XLIFF", "Merge XLIFF"}

-- ------------------------------------------------------------
-- Allows for language/tool selection
-- ------------------------------------------------------------
set theSelection to (choose from list option with prompt "Make Selection:" without multiple selections allowed) as text

-- ------------------------------------------------------------
-- Begin of if-statements for project options
-- ------------------------------------------------------------
if theSelection is "Full project" then
	
	
	# create project folder
	
	set project_location to choose folder with prompt "Customer documents :" default location "/Users/bowjest/01 Translations/02 Customers"
	set project_name to text returned of (display dialog "Job name :" default answer "")
	tell application "Finder"
		set project_folders to make folder at project_location with properties {name:project_name}
		make new folder at project_folders with properties {name:project_name}
		
		#set label index of project_location to 6
		#set label index of project_folders to 6
	end tell
	
	# create project internal folders
	
	set project_folders_list to {"originals", "mail", "translation"}
	repeat with i from 1 to number of items in project_folders_list
		set this_item to item i of project_folders_list
		set xlf_file to (path to home folder as text) & "01 Translations:02 Customers:01 FIRM:de-gb.template.xlf"
		tell application "Finder"
			make new folder at project_folders with properties {name:this_item}
			copy xlf_file to project_location
			#copy file "/Users/bowjest/01 Translations/02 Customers/01 FIRM/cafetran.project.xlf" to project_location
		end tell
	end repeat
	
	tell application "Finder" to open project_folders
	
	# save job mail to /mail/
	# save attachments to /originals/
	
	
	set mail_data to button returned of (display dialog "Save job data from selected mail  ?" buttons {"Yes", "No"} default button {"No"})
	if mail_data = "No" then
		display alert "Job created"
	else
		tell application "Mail"
			set job to item 1 of (get selection)
			set job_info to (get source of job)
			set job_name to ((get subject of job) & ".eml")
			set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
			set job_name to text items of job_name
			set AppleScript's text item delimiters to "_"
			set job_name to job_name as rich text
			set AppleScript's text item delimiters to ASTID
			set job_path to open for access file ((project_folders as rich text) & "mail:" & job_name) with write permission
			write job_info to job_path
			close access job_path
			repeat with original in every mail attachment in job
				set original_name to (get name of original)
				tell original
					set original_path to POSIX path of ((project_folders as rich text) & "originals:" & original_name)
					save original in (POSIX file original_path)
				end tell
			end repeat
		end tell
	end if
	
	# create project folder
	
	#set parameters_data to ("<?xml version=\"1.0\"?><xliff version=\"1.2\" xmlns=\"urn:oasis:names:tc:xliff:document:1.2\" xmlns:text=\"urn:cafetran:text\" xmlns:bill=\"urn:cafetran:bill\" xmlns:property=\"urn:cafetran:property\" property:Client=\"FIRM\" text:segment=\"0\"/>")
	
	# write contents to file and close
	
	#set parameters to open for access file ((project_folders as text) & "cafetran.project.xlf") with write permission
	#set eof parameters to 0
	#write parameters_data to parameters as «class utf8»
	#close access parameters
	
	# new CafeTran project is created
	
	tell application "Finder" to open project_folders
	
	#display alert "Job created"
	
end if

You’ll notice I’ve commented out several things I’ve tried (I’ve still included them here in case I was close in some way).

Also, it would really be great if I could get the section above where I actually try to create the .xlf file itself using AppleScript, but when I do this, CafeTran doesn’t seem to recognise it as valid. The parameters are the same as in the file produced by the system (i.e. CafeTran itself), but I can only guess I’ve somehow failed to include the right defining parameters to make it a valid UTF-8 when the script completes.

Can anyone advise on how I can either:

  1. Get the above section of the script to create a default .xlf file:
#set parameters_data to ("<?xml version=\"1.0\"?><xliff version=\"1.2\" xmlns=\"urn:oasis:names:tc:xliff:document:1.2\" xmlns:text=\"urn:cafetran:text\" xmlns:bill=\"urn:cafetran:bill\" xmlns:property=\"urn:cafetran:property\" property:Client=\"FIRM\" text:segment=\"0\"/>")
	
	# write contents to file and close
	
	#set parameters to open for access file ((project_folders as text) & "cafetran.project.xlf") with write permission
	#set eof parameters to 0
	#write parameters_data to parameters as «class utf8»
	#close access parameters

or

  1. Copy a pre-named default .xlf file to the project folder itself. The hierarchy is like this:

Overall project folder (which contains)
Project folder
Mail
Source
Translation

I need to copy the template .xlf file into the “Project folder” that is on the same level with the Mail, Source and Translation folders.

Many thanks,

Bowjest

Hi,

I refactored the Mail block a bit by putting the standard additions commands out of the application tell block and adding basic error handling for the write-to-disk part



if mail_data = "No" then
	display alert "Job created"
else
	set projectFolderHFS to project_folders as text
	tell application "Mail"
		set job to item 1 of (get selection)
		tell job
			set job_info to source
			set job_name to subject & ".eml"
		end tell
	end tell
	set {TID, text item delimiters} to {text item delimiters, ":"}
	set job_name to text items of job_name
	set text item delimiters to "_"
	set job_name to job_name as text
	set text item delimiters to TID
	set jobPath to (projectFolderHFS & "mail:" & job_name)
	try
		set fileRef to open for access file jobPath with write permission
		write job_info to fileRef
		close access fileRef
	on error e
		try
			close access file jobPath
		end try
		display dialog e
		-- do further error handling
	end try
	tell application "Mail"
		repeat with original in every mail attachment in job
			set original_name to (get name of original)
			set original_path to (projectFolderHFS & "originals:" & original_name)
			save original in file original_path
		end repeat
	end tell
end if

by the way, the Finder command to copy a file is duplicate, copy is used by AppleScript itself to assign a value to a variable

Thanks, StefanK! Much appreciated!

I’ve integrated that into my existing script, but find that when I tried “duplicate” rather than “copy” I get the following:

# create project internal folders
	
	set project_folders_list to {"originals", "mail", "translation"}
	repeat with i from 1 to number of items in project_folders_list
		set this_item to item i of project_folders_list
		set xlf_file to (path to home folder as text) & "01 Translations:02 Customers:01 Hays:de-gb.template.xlf"
		tell application "Finder"
			make new folder at project_folders with properties {name:this_item}
			#duplicate xlf_file to project_location
			#copy file "/Users/will/01 Translations/02 Customers/01 Hays/cafetran.project.xlf" to project_location
		end tell
	end repeat

This produces the following error message:

error “Finder got an error: An item with the same name already exists in this location.” number -15267

I take it that the system things I’m trying to make the file the folder rather than “copy” or “duplicate” it to the named folder.

And I still can’t understand what I’m not getting right with trying to create the .xlf file via the script. It completes fine and I can open the project, but when I load a document into the project it just doesn’t acknowledge that I’ve added anything to the project.

If, however, I manually copy over my default .xlf file (created by CafeTran itself), I don’t have this problem.

This part of the script works fine for creating my OmegaT projects, but doesn’t seem to get the job done with CafeTran.

Any ideas?

Thanks,

Bowjest

Your copy instruction ask the Finder to use an Unix path which it can’t work with.

The Finder’s dictionary is fair enough to give you the needed informations :

duplicate‚v : Duplicate one or more object(s)
duplicate specifier : the object(s) to duplicate
[to location specifier] : the new location for the object(s)
[replacing boolean] : Specifies whether or not to replace items in the destination that have the same name as items being duplicated
[routing suppressed boolean] : Specifies whether or not to autoroute items (default is false). Only applies when copying to the system folder.
[exact copy boolean] : Specifies whether or not to copy permissions/ownership as is
→ specifier : to the duplicated object(s)

If you are OK to destroy an already existing file in the folder project_location, it’s your duty to tell it to the Finder by adding “with replacing” to your instruction.
If you don’t accept to destroy the existing file, it’s your duty to define a unique name for the new file.

Yvan KOENIG (VALLAURIS, France) dimanche 8 juin 2014 17:51:35

Hi Yvan,

I think I understand what you mean.

I’ll give it a try and see what I come up with.

Thanks for your help.

Bowjest