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
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.
Choose file and move to known folder location. (Done)
Get name of file. (Need)
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.)
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
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
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
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
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