Apple Script: how to delete a file - applescript

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.

Related

Mac OS Delete files from folder with name recursively

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/*"

ApplescriptObjC Path To Folder

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

File associations in OS X: doesn't pass filename in args?

I am packaging an application into a .app directory for "drag install" or whatever it's called and I have a weird iessue with file association.
I set my application as a viewer for .xyz files, and the system does start my app when I double click that file; the only problem is that the path of the file I clicked is nowhere in the args[], there's only one parameter that is something like ~psn_0_901340 and I think is a timestamp because it changes every time.
So... what am I supposed to do? I've been sitting here for 2 hours straight and can't find a solution.
I think what you want is an AppleScript droplet.
A shortened version of the AppleScript from that link:
on open dropped_files
set the_command to quoted form of POSIX path of (path to resource "script.sh")
set file_list to ""
repeat with file_path in dropped_files
set file_list to file_list & " " & quoted form of POSIX path of file_path
end repeat
set the_command to the_command & file_list
do shell script the_command
end open
Export as an application using Script Editor. Place script.sh in the Resources folder.
Add your file extension associations to Info.plist. You may need to launch or move the droplet before OS X notices the change & allows you to double-click files.
If you want to launch Terminal or capture the script output, see the full AppleScript.

Apple Script - Selecting files, exclude type, zip it

I need to zip the content of a folder (and all the subfolders) for hundreds of folders.
Is it possible to run a command that takes all the files of a specific folder (prompt), except all the files that have a .fla extension and zip this content into one zipfile?
Right now I am copying the folder, search for all the .fla-files, then select all the files inside the folder (I have the to zip the content, not the folder) and create a zip of it (takes way too long.
I know that it is possible to use Apple Script to delete and copy files. But does this also work in the above mentioned order + zipping?
Alright, so I was still kind of stuck with this issue.
I created a Bash Script, that is executed via an Applescript executable File that has only one line of code:
do shell script "/Volumes/Work/createZipFile.sh"
The Bash Script opens Applescript which lets me prompt a folder (I know, kind of silly to open AS to run a Bash Script that runs AS). The variable is then used to zip this folders content without the .fla files.
myFolder=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfolder to choose folder with prompt "Select the Folder that you want to zip!"
end tell
return (posix path of myfolder)
EOT`
cd $myFolder
zip -r ZipMe.zip . -x ".fla"
echo "A zip File has been created"
So this script does actually work for some folder I try to zip.
But unfortunately not for every folder I chose. Sometimes (no idea why) it seems like it can not find the folder I chose with the prompt, so I starts (at least the zip-process starts running like crazy and doesn't stop) zipping my whole drive.
Any ideas what could be wrong?
In case anybody wants to use this script (which I highly doubt ;)), here is my final version of it.
#!/bin/bash
#Opens an applescript prompt window to select a folder
myFolder=`/usr/bin/osascript << EOT
tell application "Finder"
activate
set myfolder to choose folder with prompt "Select the Folder that you want to Zip!"
end tell
return (posix path of myfolder)
EOT`
# Terminate if the path is empty (canceled)
if [ -z "$myFolder" ];
then
#echo "Chose a folder!"
exit 0
else
#Change the directory to the above selected folder
cd "$myFolder"
# Creates a ZipFile with todays date of the selected folder, neglecting the after -x listed filetypes
zip -r ZipFile_`eval date +%Y_%m_%d`.zip . -x "*.fla*" "*.AppleDouble*" "*.DS_Store*"
#echo "A zip File has been created"
fi
Your first step should be to figure out what "Kind" of file the .fla is. To do this, run this script and select one of your .fla files:
tell application "Finder"
set theFile to choose file
display dialog (kind of theFile) as string
end tell
And then to get all of the files BUT that type in any folder, you can run this script (Replacing "Plain Text" with whatever type your .fla's turn out to be):
tell application "Finder"
set thePath to choose folder
set theFiles to get every file of folder thePath whose kind is not equal to "Plain Text"
end tell
from there it's just a matter of zipping. After doing some quick googling it looks like the easiest way to zip from applescript is by using do shell script, which shouldn't be that bad now that you have all the files you need in a nifty little array. If you're going for speed though, I might suggest moving this whole project over to bash. That should also simplify things quite a bit. Best of luck!

Delete file without playing sound in Applescript?

tell application "Finder"
set deletedfile to alias "Snow Leopard:Users:test.pdf"
delete deletedfile
end tell
The problem is I repeatedly call this script from my Cocoa application so the sound is played repeatedly too. Is it possible to disable that sound ?
Since the trash is just an invisible folder inside your home folder you can do this...
set myFile to (path to desktop folder as text) & "myFile.txt"
set trashFolder to path to trash folder from user domain
do shell script "mv " & quoted form of POSIX path of myFile & space & quoted form of POSIX path of trashFolder
one simple way (doesn't move to Trash)
do shell script "rm '/Users/test.pdf'"

Resources