Applscript *.type - macos

I know I'm being an idiot here... Where am I going wrong? is it neccicary to use sudo rm -f? and why is the *.cr2 not working?
tell application "System Events"
try
do shell script "rm /Users/splashretouch8/Pictures/SplashNW/Capture/*.cr2"
end try
end tell

Here is another approach:
set theFolder to alias ("/Users/splashretouch8/Pictures/SplashNW/Capture/" as POSIX file)
tell application "Finder" to delete (every file of theFolder whose name contains ".cr2")
Or
do shell script "cd '/Users/splashretouch8/Pictures/SplashNW/Capture/' ; rm *.cr2"

Put a "\" character in front of the asterix (e.g. "\*.cr2") and see if it works better for you.
Myself, I think using "rm" is way dangerous in an Applescript. It might be smarter to move files you want to remove into the Trash.

Related

Applescript to copy files based on partial file name

Forgive me, I'm a bit of an amature.
I'm working to comply with a records request for hundreds of students. All files are named with the first 5 digits of the name being the student's ID number. I created the script below and it runs but with no results.
I would welcome any help that you guys can provide.
with timeout of 3600 seconds
tell application "Finder"
set myFiles to files of folder POSIX file "/Volumes/Storage/Records" as alias list
end tell
repeat with aFile in myFiles
tell application "System Events"
set myvalues to {"11111", "22222", "33333", "44444", "55555", "66666", "77777", "88888", "99999", "00000", "11112", "22223", "33334", "44445", "55556", "66667", "77778", "88889", "99990"}
if name of aFile contains myvalues then
copy aFile to folder POSIX file "/Volumes/Storage/Records"
end if
end tell
end repeat
end timeout
First of all, the command to copy files in terminology of Finder and System Events is duplicate.
Second of all (and the main issue) you have to check the first 5 characters not the entire name.
And third of all, it’s not quite useful to copy the files to the same place but I guess it’s only a placeholder
property IDNumbers : {"11111", "22222", "33333", "44444", "55555", "66666", "77777", "88888", "99999", "00000", "11112", "22223", "33334", "44445", "55556", "66667", "77778", "88889", "99990"}
with timeout of 3600 seconds
tell application "Finder"
set myFiles to files of folder "Storage:Records:"
repeat with aFile in myFiles
set IDPrefix to text 1 thru 5 of (get name of aFile)
if IDPrefix is in IDNumbers then
duplicate aFile to folder "Storage:Records:Destination:"
end if
end repeat
end tell
end timeout
My solution uses only the Finder because it doesn't include the invisible files and it's not needed to coerce the Finder object specifiers to alias.
If the location of the files is on an external volume, it’s easier to use the HFS path “Storage:Records” than the coercion from POSIX path POSIX file "/Volumes/Storage/Records” as alias
I suggest you to use do shell script function instead of using Finder application as it's more flexible solution. And you can find any errors in your command easily because you can debug it using the Terminal.
do shell script "<your commands here>"
For your use case you need to use a couple of commands inside do shell script: find and then cp.
I would suggest a simple bash script is a better option here. Save the following script on your Desktop as CopyFiles
#!/bin/bash
# Make a subdirectory to copy the results to
mkdir results 2>/dev/null
# Read all ids from file "ids.txt"
while read id; do
echo Processing id: $id
# Remove the word "echo" on following line to actually copy files
echo cp /Volumes/Storage/Records/${id}* results
done < ids.txt
It assumes there is a file on your Desktop called ids.txt that looks like this
11111
22222
12345
54321
Then start a Terminal, by pressing Cmd+Spacebar and typing "Terminal" and hitting "Enter".
Go to your Desktop and make the script executable with
cd Desktop
chmod +x CopyFiles
and then run it with
./CopyFiles
At the moment, it does nothing except tell you what it would do like this:
Processing id: 11111
cp /Volumes/Storage/Records/11111* results
Processing id: 22222
cp /Volumes/Storage/Records/22222* results
Processing id: 12345
cp /Volumes/Storage/Records/12345* results
Processing id: 54321
cp /Volumes/Storage/Records/54321* results
If it looks like it is doing what you want, edit the script and remove the word echo where noted and run it again.

Automator/Applescript + shell script: grab parent folder name

I have a Automator workflow that utilizes this shell script to grab the name of the directory hosting the file running through this workflow. Later I place that directory name as comment for the file.
for f in "$#"
do
filepath=$(dirname "$f")
dirname=$(basename "$filepath")
echo "$dirname"
done
Whenever I throw multiple files at it though, the directory name gets reflected not once (as I would like to) but times however many files I dropped at it. This then later adds the same comment that many times.
How do I fix that?
EDIT:
I want to try eliminate Automator and go with Applescript + Shell alone.
How do I have the shell return the directory name? Right now it just gives me $dirname in the dialog...
on adding folder items to theWatchedFolder after receiving theDetectedItems
set dirName to do shell script "for f in '$#'
do
filepath=$(dirname '$f')
dirname=$(basename '$filepath')
echo '$dirname'
done"
display alert dirName
end adding folder items to
on adding folder items to thisFolder after receiving added_items
repeat with aFile in added_items
tell application "Finder"
set parentpath to POSIX path of (parent of (aFile) as string)
set comment of aFile to parentpath
end tell
end repeat
end adding folder items to
I would go with an Applescript droplet.
Save this code as an Application.
When you drop files onto it from a single dir or multiple, it will comment each file with it's own original dir.
Then move the file to the listed move folder.
property moveFolder : "Macintosh HD:Users:USERNAME:fooDIR:"
on open theseFiles
repeat with i from 1 to number of items in theseFiles
set this_item to item i of theseFiles
tell application "Finder"
set parentpath to POSIX path of (parent of (this_item) as string)
set comment of this_item to parentpath
end tell
end repeat
tell application "Finder"
move theseFiles to moveFolder
end tell
end open
You could use a choose command to choose where to move the files instead of hard coding but the files may not be always handed of to the droplet in a single batch even though thats how you dropped them on to it. This means the `choose dialog may display multiple times on whats seems a single run.
But the above hopefully gives you a starting place.

How to move a file to trash in AppleScript?

I know its a dumb question but somehow the command i type is not working
set deleteFile to srcTemp & "Archive.zip"
--deleteFile path is something like this /Users/home/Desktop/Archive.zip
tell application "Finder"
move POSIX file "" & deleteFile & "" to trash
--move file "\"" & destNoQuote & "Archive.zip\"" to trash
empty the trash
end tell
But I get an error saying can't find the POSIX file.
I know that many people use the posix file command inside the Finder tell block of code however that's a mistake. The posix file command is not a Finder command, it's an applescript command, and therefore should not be in the Finder block if possible. This is true for all commands actually. You should only tell an application to perform the commands you can find inside of its applescript dictionary otherwise you will see unexpected behavior... as you are finding.
As such this is how you should write your code...
set deleteFile to srcTemp & "Archive.zip"
set posixFile to POSIX file deleteFile
--deleteFile path is something like this /Users/home/Desktop/Archive.zip
tell application "Finder"
move posixFile to trash
empty the trash
end tell
Here's another method of your entire script in one line:
do shell script "rm -Rf " & quoted form of (srcTemp & "Archive.zip")
This will force remove your file and it doesn't just go to your trash, it's gone.

How to pass file path to AppleScript in Automator?

I want to be able to right click on a file or folder in Finder and select Services > Open Terminal to open a terminal window at that path.
In automator, I have a service that runs an AppleScript
tell application "Terminal"
do script "cd $filePath"
activate
end tell
I don't know how to pass the file path!
Bonus: How can I ensure that this works for both files and folders? If its a file, it may say that the file is not a directory.
Bonus: Where could I have found this answer myself? The documentation seems to be way to dense.
Thanks
Chet
Note the "Service receives selected ... " at top, which gives result to applescript.
This will open folders and containers of files, but won't be redundant.
on run {input, parameters}
set didThese to {}
repeat with f in input
set f to (f as text)
if f ends with ":" then
if f is in didThese then --check to see if we did this already
--ignore
else
tell application "Terminal" to do script "cd " & quoted form of POSIX path of f
set didThese to (didThese & f) --load into memory for subsequent iterations of loop
end if
else
--first get containing folder, then use that
tell application "Finder" to set f to ((container of alias f) as alias as text)
if f is in didThese then
--ignore
else
tell application "Terminal" to do script "cd " & quoted form of POSIX path of f
set didThese to (didThese & f)
end if
end if
end repeat
activate application "Terminal"
end run
Gleaned from https://apple.stackexchange.com/questions/112731/automator-service-to-print-a-relative-path-of-selected-files-printing-everything
[edit:]
Incidentally, one thing left to decide is how to treat bundles, like .app files, which aren't really files. Using
set i to info for (alias f)
then
package folder of i
will enable the script to determine this, through an additional if/then branch. I personally don't mind it "cd"ing into bundles.

Applescript: Get directory dropped on

I have a applescript with the following:
on open dir
tell application "Finder"
tell application "Terminal"
activate
tell application "Terminal"
do script "cd " & dir
end tell
end tell
end tell
end open
It works, and gets the directory thats dropped on it, but it cd's into cd Macintosh HD:Users:USER:Desktop:C Files:
Is there any way to replace : with / ? in applescript?
Found the answer myself!:
set dir to quoted form of POSIX path of dir

Resources