I have a main directory with projects. Each project is a folder, like myfolder/projectname. Each project has a directory called var and another directory there called cache, like myfolder/projectname/var/cache. I would like to prepare a script in Apple script to run Finder recursively through myfolder folder, find there all project folders that are like var/cache and then remove all files from those folders. For instance:
myfolder/projectname1/var/cache
myfolder/projectname2/var/cache
myfolder/projectname3/var/cache
How to achieve this? For now I have a code like this but is not working
try
tell application "Finder"
delete (every item of folder ("/Users/myuser/myfolder") whose name is "cache")
end tell
on error
display dialog ("Error. Couldn't Move the File") buttons {"OK"}
end try
This can be accomplished simpler and more efficient with the shell.
* are wildcards, the first considers all folders in myfolder the second all items in cache. quoted form is needed if the real name of myfolder contains space characters.
set baseFolder to POSIX path of (path to home folder) & "myfolder/"
do shell script "rm " & quoted form of baseFolder & "*/var/cache/*"
Related
I've been trying to find a program or some sort of app to run all the time and check one folder. If any document would be saved in the folder it would rename it like file1.png the next one file2.png.
Transnomino has an automation feature that allows to automatically rename any file dropped in a folder using a specified renaming Recipe. The Apple Script that does this looks something like this:
on adding folder items to theAttachedFolder after receiving theNewItems
tell application "Finder" to set thePath to POSIX path of theAttachedFolder
return do shell script ¬
"open -a Transnomino --args -d \"" & thePath & "\" -r \"" & thePath & ".recipe\""
end adding folder items to
A complete guide how this can be setup is described on the website of Transnomino. Here: https://transnomino.bastiaanverreijt.com/automation/index.html
I'm trying to write an AppleScript that will simply copy the contents (both folders and files) from a specified source folder to a specified destination folder. At the moment my script runs but only copies one file and I can't work out how to get it to copy all files in the folder.
Here's my script:
set sourceFolder to (POSIX file "/Users/benny/Desktop/Projects/Source/Project1") as alias
set destinationFolder to (POSIX file "/Users/benny/Documents/Masters/Project1") as alias
tell application "System Events"
set availableSourceFiles to every file of sourceFolder whose visible is true
set filesOfTargetFolder to files of destinationFolder whose visible is true
end tell
-- if no more source file is available, quit this script
if (count of availableSourceFiles) = 0 then
quit
end if
set sourceFile to (first item of availableSourceFiles) as alias
-- use the Finder to copy the file
tell application "Finder"
-- duplicate the file to the target folder
duplicate sourceFile to destinationFolder
end tell
I'm assuming I need to include a for each type loop but can't get the syntax correct here. Haven't written AppleScripts in many years so trying to remember how it all works.
If the destination "Project1" folder doesn't have stuff in it already, then duplicating the folder is likely to be quicker:
tell application id "com.apple.Finder" to duplicate folder POSIX file ¬
"/Users/benny/Desktop/Projects/Source/Project1" to the folder ¬
POSIX file "/Users/benny/Documents/Masters" with replacing
However, if that's not an option, then I'd stick with your method and copy the contents of the folder across instead:
set here to POSIX file "/Users/benny/Desktop/Projects/Source/Project1"
set there to POSIX file "/Users/benny/Documents/Masters"
tell application id "com.apple.Finder" to duplicate ¬
every item in the folder here to there
Bear in mind that if there's a file or folder at any of the destinations that intended to be occupied by one of the incoming source items, Finder will throw an error. You would typically incorporate some sort of check ahead of the copy.
I'm trying to write a script that deletes all .tmp files in a chosen folder as well as all of its subfolders.
I currently have it set up as such:
set sequenceFolder to choose folder with prompt "Please select a sequence:"
try
tell application "Finder"
set SubFolders to every folder of entire contents of sequenceFolder
repeat with aFolder in SubFolders
delete (every item of folder (sequenceFolder) whose name ends with ".tmp")
delete (every item of folder (SubFolders) whose name ends with ".tmp")
delete (every item of folder (aFolder) whose name ends with ".tmp")
display dialog ("Files Deleted!") buttons {"OK"}
end repeat
end tell
on error
display dialog ("Error. Couldn't Delete Files") buttons {"OK"}
end try
It currently works at deleting files in sequenceFolder but not any of the subfolders. I would also love for it to look for subfolders recursively, but I have no idea how. Please help!
I recommend to use the shell commands find and rm because they provide to consider all files in the given folder and its subfolders and it's so much faster than the Finder.
Caution:
The following code deletes all files with specified file extension in all folders and subfolders of the selected folder immediately without putting them in the trash folder
set theFolder to POSIX path of (choose folder with prompt "Please select a sequence:")
do shell script " /usr/bin/find " & quoted form of theFolder & " -name '*.tmp' -exec rm {} \\;"
display dialog ("Files Deleted!") buttons {"OK"}
I am trying to delete a hidden file that shows up every time I restart my computer with an Apple Script set to run on startup. I can't however seem to be able to correctly guess the path of this file.
The file's path is Macintosh HD/Users/cristian/Dropbox (Hyperion)/Hyperion Team Folder/Icon
If I move the file to the desktop and run the script bellow, it works.
tell application "Finder"
delete the file "Icon
" of the desktop
end tell
My question is, how do I change this script to target the path above?
Also, is there anyway to permanently delete it not just move it to the trash?
Thanks in advance.
Assuming there is no new line character at the end of the file name this code deletes the file in the Dropbox folder and empties the trash.
Be aware that the empty trash command affects all items in the trash not only the currently deleted file.
set iconFile to ((path to home folder as text) & "Dropbox (Hyperion):Hyperion Team Folder:Icon"
tell application "Finder"
delete file iconFile
empty trash
end tell
Alternatively use the shell to delete the file, in this case the file will be deleted immediately.
set iconFile to POSIX path of (path to home folder) & "Dropbox (Hyperion)/Hyperion Team Folder/Icon"
do shell script "/bin/rm " & quoted form of iconFile
just use a do shell script command "rm" which delete file directly (without transfer to trash), like in script bellow :
Set myFile to "Macintosh HD/Users/cristian/Dropbox (Hyperion)/Hyperion Team Folder/Icon"
try
do shell script "rm " & quoted form of myFile
end try
However, it should be better to understand root cause why this file is added every time, and then address this root cause.
I am currently setting up an ApplescriptObjC application. Whenever I try other methods, it screws up. I'm trying to set it up where a shell script uses the mv command to move a file from the "Files" directory to the /usr/bin/ folder. I think it would go a little something like: do shell script "sudo mv " & path & "/Files/ /usr/bin/" where path would be the path to me. I have tried the path to me and posix and other stuff, just doesn't work. ![The Files folder contains a file I want to move to /usr/bin] Image of where the folder is: http://i.stack.imgur.com/jUX6w.png
First of all the folder Files in your screenshot is a virtual folder in Xcode (yellow).
You have to create a real folder (blue). The easiest way is to drag a folder in Finder to the Xcode sidebar and select "Create folder references"
To use sudo in AppleScript append with administrator privileges to the do shell script line. You will be prompted to enter an admin password.
This code moves(!) the files to /usr/bin. If you want to copy (duplicate) the files use cp -r instead of mv
set filesFolder to (current application's NSBundle's mainBundle()'s resourcePath()'s stringByAppendingPathComponent:"Languages") as text
tell application "System Events" to set filesToMove to name of files of folder filesFolder
repeat with aFile in filesToMove
do shell script "/bin/mv " & quoted form of (filesFolder & "/" & aFile) & space & "/usr/bin/" with administrator privileges
end repeat