Copy file to folder of application

Hello, I’m new to Applescript.

I want to choose a file and copy it to the folder where the applescript app is located.

I wrote this script, but is does not work:

tell application "Finder"
	get POSIX path of (container of (path to me) as text)
	set myFolder to result
end tell
		
set myFile to choose file with prompt "Choose file"

tell application "Finder"
	duplicate myFile to myFolder
end tell

Any suggestions?

Hi,

AppleScript expects HFS paths (colon separated), not POSIX paths (slash separated)


tell application "Finder"
	set myFile to (choose file with prompt "Choose file")
	duplicate myFile to container of (path to me)
end tell

Note: the coercion as text is not needed, path to me is an alias and the Finder can get the container of it

or, in one line


tell application "Finder" to duplicate (choose file with prompt "Choose file") to container of (path to me)

StephanK, Thank you! That’s what I needed.

Now, I’ve run into another problem, I also want to get the name of the file that was chosen to use in a shell script.

I tried this line but I get AS error message.

set myName to name of (myFile as text)

Just for an overall picture of what I’m trying to do.

  1. Choose file and move to known folder location. (Done)
  2. Get name of file. (Need)
  3. Run shell script on file. (Will work if the file name and path is already hard coded in script. However I want the file name to be variable. By doing step 1, I already know the path to the file.)

Thanks again.

the error occurs, because you try to get the name of a string (path), this is not possible

use also the Finder to get the name


tell application "Finder"
	set myFile to (choose file with prompt "Choose file")
	set myName to name of myFile
	duplicate myFile to container of (path to me)
end tell

StefanK, Thank you so much. It works just as I wanted. I’ll be sure to credit you in the program.

Here is how it is used in xCode

if name of theObject = "choosefile" then
		tell application "Finder"
			set myFile to (choose file with prompt "Choose file")
			set myName to name of myFile
			duplicate myFile to container of (path to me) with replacing
		end tell
	
		set cmd_string_create to "cd " & (safe_path as string) & "edition/install; mono onisplit.exe -create:txmp ../../ModTXMP -format:bgr32 -genmipmaps " & " ../../" & myName
		do shell script cmd_string_create
		
		display dialog "TXMP.oni file created"
		
	end if

I think my next step would be to try and make a batch process. Take all the files in a folder and then run the conversion script on it.

FYI, There are a group of programmers who are working on an unofficial Anniversary Edition of Bungie’s game Oni. A key tool used to modify the game’s data file is called OniSplit. It runs under the Mono framework in OSX, however, it is command line only. So I’m trying to make an Application in xCode to run OniSplit. What’s cool is that it takes a user’s original game data and patches it to the Anniversary Edition.

Now I would like to copy a folder to the same directory as the application but now rename the copied folder.

I did this, but it renames the original folder, not the copied folder.

tell application "Finder"
			set myFolder to choose folder with prompt "Choose folder"
			duplicate myFolder to container of (path to me) with replacing
			set name of myFolder to "ReNamed"
end tell

Thanks again.

the result of the duplicate line is a reference to the folder at the new location


tell application "Finder"
	set myFolder to choose folder with prompt "Choose folder"
	set newFolder to duplicate myFolder to container of (path to me) with replacing
	set name of newFolder to "ReNamed"
end tell

StefanK: You’re a great help! Thanks.

Now if I can pick your brain one more time.

Going back to the original script:

tell application "Finder"
   set myFile to (choose file with prompt "Choose file")
   set myName to name of myFile
   duplicate myFile to container of (path to me)
end tell

Currently the files copies to Oni: (This is where the app is located), but if possible I would like it to go to Oni:ModTXMP

no problem


tell application "Finder"
	set myFile to (choose file with prompt "Choose file")
	set myName to name of myFile
	duplicate myFile to folder ((container of (path to me) as text) & "ModTXMP:")
end tell

Thank you very much

Grrr. I just realized that if the folder ModTXMP does not exist, the script will not work.
How do I make the folder ModTXMP at the location?

This did not work:

set name of folder (container of (path to me)) to "ModTXMP"

Also, on the other script, I discovered with the renaming of the folder if that folder already exists, AS fails with error.

This also does not work:

set name of newFolder to "ModTXMP" with replacing

I’m trying two approaches: the first is to convert a single image file to a data file for Oni. The second is a batch process, take a folder of images and convert all them to Oni data files. I have the script commands working to do it, I’m just having a hard time with the correct AS syntax.

I’m trying to make everything as easy as possible for the end user, so that they don’t have to think about which folder to put things in or to the make a folder, etc.

Finally, can you recommend a book or website so I can learn the correct commands and syntax for Applescript?

Sorry to keep asking you these questions. It seems as I make more progress, I get more ideas or other unexpected things come up.


tell application "Finder"
	set parentFolder to container of (path to me)
	if not (exists folder "ModTXMP:" of parentFolder) then
		make new folder at parentFolder with properties {name:"ModTXMP"}
	end if
	set myFile to (choose file with prompt "Choose file")
	set myName to name of myFile
	duplicate myFile to folder "ModTXMP" of parentFolder
end tell

on ScriptWire you can find a bunch of tutorials about AppleScript.
Recommendable books are Hanaan Rosenthal’s Comprehensive Guide and Matt Neuburg’s The Definitive Guide

StefanK, Thank you so much for your timely assistance and your recommendations for learning Applescript.

The syntax parentFolder will be very useful for me in the future.