Copy A File In AppleScript - file

I have the code below to set a variable in Applescript for the path to the iTunes Music Folder:
set username to text returned of (display dialog "RingtoneDude" default answer "Enter your path to your iTunes Ringtones folder here. e.g. /Users/David/Music/iTunes/iTunes Music/Ringtones" buttons {"Confirm", "Cancel"} default button 1)
And then I have the code to call the variable username to copy a file
tell application "Finder"
copy file theCopy to username
end tell
but the file theCopy which is on the desktop (theCopy is a variable) does not move to the folder.
Please help.

I believe you've misunderstood the copy command. The copy command is used for transferring the contents of one variable into another variable. What you need to use is the duplicate command, which is used for copying files to a specified location. Its syntax is as follows:
duplicate [alias] to [alias]
[replacing boolean] --If true, replaces files in the destination with the same name
Having said that, your new code, in conjunction with Uriah Carpenter's answer, should look something like this:
set myRingtoneFolder to choose folder with prompt "Select your iTunes Ringtone folder:"
tell application "Finder" to duplicate theCopy to myRingtoneFolder

I would suggest you use choose folder which returns an alias.
To make some text into an alias object use set myAliasPath to myTextPath as alias.
For more detailed information see Aliases and Files in the AppleScript documentation.

Related

How do I remove an Error.error file being created and cannot be deleted?

How do I delete/remove the "Error.error" file when the system says it is not there but it is?
The Error.error file was created somehow and I do not know why or how. However, the issue is the system has created this 0 byte file called "Error.error" and it cannot be deleted, even if I try a "permanent" delete command. It also will not allow me to delete any of the folder directory hierarchy in which is resides. When I do try create it, rename it, or create a physical file with the same name and extension, or try to delete this file directly or indirectly I receive the following error message:
"Could not find this item: This is no longer located in C:\maps\test. Verify the items location and try again." Then it list "Error.error" and its information. However, it is there visually.
Any thoughts or suggestions on how to remove or correct?
Thanks!
I had a very similar issue not too long ago where I had a file that was created by another program and whenever I tried to access or delete it, windows would give an error message like "...This is no longer located in...".
If you're having the same problem I was, here's how I got rid of the file. Start a command prompt. In the command prompt, enter a command like del "\\?\<full path to file>". So if your file name is "Error.error" and it is located in the folder "C:\maps\test", the command you would enter is
del "\\?\C:\maps\test\Error.error"
You can delete a folder that windows won't let you access in a similar way by changing the command from "del" to "rd /S".
If you don't know how to start a command prompt, simply click the start button and type "command". The start menu should offer an option named something like "Command Prompt - Desktop app". That's what you need to start.
I found a good explanation of what the "\\?\" means here.

Copy an image and save as new name in different folder

I have an image. Say its
Cutedoggo.png which is sitting on my desktop
I would like to copy it over, rename it , and save it to a new destination. Let's say I want to rename it to Dog1.png in the c:/doggo folder I've made
I tried in windows command prompt (while inside the directory of original file)
copy CuteDoggo.png Dog1.png c:/doggo but it didn't work. Error message
error: the syntax of name is incorrect
how would I accomplish this using command prompt?
copy "CuteDoggo.png" "c:\doggo\Dog1.png"
also this works as well: (simplified)
copy CuteDoggo.png c:\doggo\Dog1.png EDIT this will not work if there's a space in folder.
courtesy of stephan

Changing file name while copying files in batch script

I want to copy the files from current folder to another location like (CURRENT FOLDER) file name like this (F0#CGDBANG000947532#) to another location like (\10.10.10.1\BasketsIn) with user name in the file name like (F0#CGDBANG000947532#logesh) at the end F0#CGDBANG000947532# copy to F0#CGDBANG000947532#username
thanks
Easy:
copy "c:\A" "d:\%username%_A"
EDIT
finally, after some of your comments here and in your other question, I understood your request (I think).
#echo off
for %%i in (%*) do if /i "%%~xi"==".eps" copy "%%i" "\\10.10.14.13\adman\in\displ\%%~ni%username%.%%~xi"
pause
If your file names do not have an extension or if you want the user name to be appended after the very end of the name (i.e. even after the extension), you could just use the following simple command:
COPY * \10.10.10.1\BasketsIn\*%USERNAME%
where USERNAME is a system environment variable that resolves to the user name of the current user.
If, however, you have names with extensions and you want the user name to be appended after the file name but before the file extension, you could use the ? mask character like this:
COPY * \10.10.10.1\BasketsIn\???????????????????????????????%USERNAME%.*
Just make sure you have provided enough ?s to cover the longest possible name in your case. If you are interested, you can learn more about this method in this excellent, in-depth Super User answer by dbenham:
How does the Windows RENAME command interpret wildcards?
One more note: this method may not work as expected with file names that have "multiple" extensions, i.e. like some.txt.doc.

How to add text documents into a file using Batch

Is it possible to create a text file and place it in an existing file using Batch? What I mean is when I type echo %input%>document.txt it obviously saves it as a text document, but what I want to happen is for it to automatically place that text document into an existing file that I have previously created. Do I have to call the folder name? I'm honestly lost on how to do this. Any help would be great!
Instead of doing
echo %input%>document.txt
You can specify the entire path of the text document
echo %input%>C:\path\to\document.txt
Obviously you need to change the path to be whatever you want.

Applescript Copy file to a new folder

I need to create an AppleSript that will copy specified files from one folder to a newly created folder.
These files need to be specified in the AppleScript editor so something like:
start
fileToBeMoved = "Desktop/Test Folder 1/test.doc"
newfoldername = "Test Folder 2"
make newfolder and name it 'newfoldername'
copy 'fileToBeMoved' to 'newfolder'
end
Generally:
tell application "Finder"
make new folder at alias "Macintosh HD:Users:user:Desktop:" with properties {name:"Test Folder 2"}
copy file "Macintosh HD:Users:user:Desktop:Test Folder 1:test.doc" to folder "Macintosh HD:Users:user:Desktop:Test Folder 2"
end tell
You can add variable names that represent POSIX files and paths.
Obviously the colon character (:) is a reserved character for folder- and filenames.
set desktopFolder to "Macintosh HD/Users/user/Desktop/"
set desktopFdrPosix to quoted form of POSIX path of desktopFolder
set newFolderName to "Test Folder 2"
set destinationFdrPosix to quoted form of desktopFdrPosix & POSIX file newFolderName
set sourceFilename to "Test Folder 1/test.doc"
set sourceFnPosix to quoted form of desktopFdrPosix & POSIX file sourceFilename
tell application "Finder"
make new folder at alias desktopFdrPosix with properties {name:newFolderName}
copy file sourceFnPosix to folder destinationFdrPosix
end tell
You may also want to add error checking if the destination folder already exists.
The trick with AppleScript is that moving files is done using aliases.
More realistically it might be easier to make a shell script instead which can be run from AppleScript using do shell script if you're using Automator or something similar.
#!/bin/sh
fileToBeMoved="$HOME/Desktop/Test Folder 1/test.doc"
newFolderName="Test Folder 2"
mkdir "$newFolderName"
cp -a "$fileToBeMoved" "$newFolderName"
set home_path to path to home folder as string
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:")
tell application "Finder"
duplicate every file of work_folder to folder "Archive" of home
end tell
This works for copying to a mounted network volume:
mount volume "afp://compname.local/mountpoint"
tell application "Finder"
duplicate file "MyTextFile.txt" of folder "Documents" of home to disk "mountpoint"
eject "mountpoint"
end tell
tell application "Finder"
make new folder at desktop with properties {name:"folder"}
duplicate POSIX file "/usr/share/doc/bash/bash.html" to result
end tell
POSIX file ((system attribute "HOME") & "/Documents" as text)
tell application "Finder" to result
tell application "Finder" to duplicate (get selection) to desktop replacing yes
-- these are documented in the dictionary of System Events
path to home folder
POSIX path of (path to documents folder)
path to library folder from user domain
path to desktop folder as text
-- getting files as alias list is faster
tell application "Finder"
files of entire contents of (path to preferences folder) as alias list
end tell
The simple way to do it is as below says
set home_path to path to home folder as string
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:")
tell application "Finder"
duplicate every file of work_folder to folder "Archive" of home
end tell
I used Chealions shell script solution. Don't forget to make your script file executable with:
sudo chmod u+x scriptFilename
Also remove the space between the = sign in the variable assignments. Wouldn't work with the spaces, for example: newFolderName="Test Folder 2"
If it's a sync you're looking for you can run the following shell script in your AppleScript:
rsync -a /Users/username/folderToBeCopiedFrom /Volumes/folderToBeCopiedTo/

Resources