I have this simple code in apple script which when run is supposed to rename the file. Instead it gives me an error Can’t get file "Volumes:Projects:Projects:1.pdf".
set ff to file "Volumes:Projects:Projects:1.pdf"
set ss to "Volumes:Projects:Projects:1.pdf"
set name of ff to ss
Three major issues.
To rename a file you have to tell Finder or System Events to do that.
A HFS path (colon separated) starts always with a disk name, never with Volumes.
To rename a file set the name property to a file name rather than to a full path.
Actually your example does nothing.
This snippet renames the file 1.pdf in folder "Projects" on disk Projects to 2.pdf
tell application "Finder"
set name of file "Projects:Projects:1.pdf" to "2.pdf"
end tell
Using the following example AppleScript code, with the existence of the file set to ff at the shown path:
set ff to POSIX path of "Volumes:Projects:Projects:1.pdf"
set ss to "2.pdf"
tell application "System Events" to set name of file ff to ss
Shows the following Event in Script Editor:
tell application "System Events"
set name of file "/Volumes/Projects/Projects/1.pdf" to "2.pdf"
end tell
Looking in Finder, 1.pdf was renamed to 2.pdf, as expected in this example.
Update: Note that vadian make a good point in 2 about the hierarchy of an HFS path, and is in part why I chose to covert it to a POSIX path. I also prefer to use System Events as it typically handle file operations faster than Finder and will work with both HFS style and POSIX style paths.
In other words, with System Events the following works:
tell application "System Events" to set name of file "Projects:Projects:1.pdf" to "2.pdf"
tell application "System Events" to set name of file "/Volumes/Projects/Projects/1.pdf" to "2.pdf"
But this too, even though malformed, works:
tell application "System Events" to set name of file "Volumes:Projects:Projects:1.pdf" to "2.pdf"
However, with Finder, only a proper HFS path works without error, it can't handle a POSIX path because it doesn't understand it as it's not in Finder's AppleScript Dictionary.
That said, one should always make sure the information being passed is properly formed even when a malformed path would work in this example with System Events, it's not a good habit to get into!
Related
I'm getting the following error:
"The file isn’t compatible with QuickTime Player."
When using this script:
tell application "Finder"
set random_file to some file of entire contents of folder "Movies" of home
open result
end tell
However, I am able to open the file from the finder manually and once I do the script works on just that file. The problem is I have thousands of files and don't want to open each one manually for the script to work again. Have not had this problem with the script in the past.
There are two ways I can think of to approach modifying your script:
Stick with Finder's open command but invoke it fully with its using parameter, which accepts an application file that informs Finder of the application that will be used to open the file. It may sound superfluous given it already tries to open it in QuickTime, and we're not trying to change that, but it's not unwise to see if it does confer a difference in behaviour:
tell application id "com.apple.finder"
tell the folder (path to movies folder) to tell (the ¬
a reference to entire contents) to tell (the ¬
some document file as alias) to set f to it
open f using application file id "com.apple.QuickTimePlayerX"
end tell
Grab the file as you like (really, you ought to be using System Events for this, not Finder, but I'll go with what you had), but then use the open handler of the specific application to open the file:
tell application id "com.apple.finder" to tell the folder (path to movies folder) ¬
to tell (a reference to the entire contents) to tell (some document file) ¬
as alias to set f to it
tell application id "com.apple.QuickTimePlayerX"
activate
open f
end tell
NB. I'm using Monterey, in which both of these two above propositions work appropriately. This doesn't necessarily infer that it will do so in Big Sur, but if they do not, it's worth checking the various app permissions under the different Security & Privacy headings of System Preferences.
I'm writing an AppleScript in which I want to insert a subroutine to delete specified files. With a flag I wish to control whether the given file is moved to the recycle bin or deleted permanently.
Actually my script looks like this:
on MyDeleteProc(theFile, allowUndo)
if allowUndo then
tell application "Finder" to delete POSIX file theFile
else
do shell script "rm " & theFile
end if
end MyDeleteProc
Now I want to know if this case is correct so far or is there maybe another Finder command or a parameter for the delete command that I overlooked so I will be able to simplify the script above?
AppleScript is a temperamental beast, and the devil is often in the details.
While #adayzdone's answer provides the crucial pointer - use of the System Events application's delete command to achieve permanent deletion, working out the exact syntax takes trial and error:
Caveat: This handler works with both files and folders - targeting a folder with allowUndo set to false therefore permanently deletes that folder's entire subtree.
on MyDeleteProc(theFile, allowUndo)
if allowUndo then
tell application "Finder" to delete theFile as POSIX file
else
tell application "System Events" to delete alias theFile
end if
end MyDeleteProc
On OS X 10.9.4 I had to do the following to make this work:
Finder context: Had to change POSIX file theFile to theFile as POSIX file (postfix form) - don't ask me why.
System Events context: Using "cast" alias with the POSIX path provided is the only form of the command that worked for me.
That said, a little tweak to your original function would make it work, too (and unless you delete many files one by one, performance probably won't matter):
Note, however, that just using rm only works with files - if you wanted to extend it to folders, too, use rm -rf instead - the same caveat re permanently deleting entire subtrees applies.
on MyDeleteProc(theFile, allowUndo)
if allowUndo then
tell application "Finder" to delete theFile as POSIX file
else
do shell script "rm " & quoted form of theFile
end if
end MyDeleteProc
Note the use of quoted form of, which safely passes the file path to the shell, encoding characters such as spaces properly.
You can use System Events to permanently delete a file.
I am trying to use a MEL script to load ANIMATION.MA file that references CHARACTER_RIG.MA. The CHARACTER_RIG.MA and ANIMATION.MA files are produced by someone else and supplied to me. The ANIMATION.MA is looking for N:/Project/Maya//char/character/CHARACTER_RIG.MA
If I open ANIMATION.MA from Maya, or use the equivalent MEL command I always get prompted with:
"Reference File Not Found"
Reference File Not Found: N:/Project/Maya//char/character/CHARACTER_RIG.MA.
[Abort File Read] [Skip] [Browse...] [Retry]
If I tap browse, and select the CHARACTER_RIG.MA then it opens perfectly. I can see it created a reference in the Reference Editor that has the Unresolved Path (N:/...) , the Resolved Path (/my/path) and the namespace and the namespaceRN.
My question is, how do I do the equivalent of the "Browse..." from MEL? I tried pre-creating a reference, but it doesn't let me set the unresolved path, so when I load the ANIMATION.MA it keeps prompting in MAYA.
file -f -options "v=0" -typ "mayaAscii" -o "/Source/project/assets/anims/ANIMATION.MA"
If you know the directory where the file is, then you can use the dirmap command. The command dirmap allows you to remap directory structures if your disk configuration changes. So in this case it would look like:
dirmap -en true;
dirmap -m "N:/Project/Maya//char/character" "/my/path";
Possibly more manageable if you have lots of mappings to do especially when moving form a windows machine to a *nix one. However it is much more useful to define your project structure because then things just work when you move, tough this may not be the best of choice for shared assets.
I ended up finding several solutions:
Rename the RIG.MA file to match the filename in ANIM.MA (they were different in my case) and put it in one of the search or project folders that MAYA uses and it will automatically find it.
or
Programatically through code (or manually) edit the ANIM.MA file to remap the file/folder of the RIG.MA to where you want to load it from. Note: You
also need to remap any other files, such as textures. I did this with
perl -pi -e 's/\Qold-path\E/\Qnew-path\E/g' ANIMATION.MA
HTH someone else.
Quick and easy, File, Project, Set and select the folder where meshes or whatever it is.
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.
I have a strange file in my file system without the extension part. The filename is "15.". The weird thing is that it is not one of those without the dot part (like just "15"), but the one with the dot but no extension ("15.") -- it is literally an illegal filename in windows, and not sure how did it get created in the first place.
I know it is a text file and it is about 15KB in size; however, due to the weirdness in name, I can't open it with any application -- I've tried to open in notepad, wordpad, etc., have tried the 'type' command to spit it out on commans shell, tried to shell-open enclosing filename in quotes, and so on -- all methods result in a 'file not found' error except the notepad, which says '15.txt' is not found.
Due to the nature of the issue and the way search engines optimize the search, it is extemely hard to search for an answer in any of the search engines online. So, I just wanted to put this question out there and see if anybody had to deal with a similar issue and have found any way to rename the file or even to change the extension.
Filenames that are valid in NTFS but cannot be used from Windows can be created when accesing disks or shares from other operating systems like Linux.
If you don't have a Linux installation at hand, then get hold of a "live" CD, boot Linux, and change the filename.
That may sound like a hassle, but Windows-only solutions (moving stuff around, deleting the directory) are even worse.
Use REN: http://ss64.com/nt/ren.html
It is a command prompt command (run > cmd > cd wherever > ren 15. 15.txt )