How can I locate and copy (to clipboard) the most recent file in a folder?
Here's the path to the folder:
/Users/myuser/Desktop/Screenshots
Try this:
tell application "Finder" to set theFiles to sort (get files of folder "Screenshots" of desktop) by creation date
set the clipboard to last item of theFiles as alias
To copy the image to the clipboard in pure AppleScript is cumbersome. It's pretty easy with a few lines AppleScriptObjC:
use framework "Foundation"
tell application "Finder" to set theFiles to sort (get files of folder "Screenshots" of desktop) by creation date
set imageFile to last item of theFiles as text
set theData to current application's NSData's dataWithContentsOfFile:(POSIX path of imageFile)
set pasteBoard to current application's NSPasteboard's generalPasteboard()
pasteBoard's clearContents()
pasteBoard's setData:theData forType:(current application's NSPasteboardTypeTIFF)
Related
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.
How would I go about checking if a file (an alias called theFile) has the same name as any other files in another folder?
I would like to have an Applescript look through all of the files in the folder and has a if and else as a result if it does match one of the names or doesn't.
Choose a folder
Get the name of theFile
Get a list of all file names in the folder
Check if the list contains the name of theFile
set theFile to alias ...
set theFolder to choose folder
tell application "System Events"
set theFileName to name of theFile
set otherFileNames to name of files of theFolder
end tell
set fileNameExists to otherFileNames contains theFileName
this is the script I'm trying to use:
on open thisItem
set this_folder to (the POSIX path of thisItem)
set export_folder to "Macintosh HD:Users:j****.*******:Desktop:AUTOMATOR:export"
set pdf_folder to "Macintosh HD:Users:j****.*****:Desktop:AUTOMATOR:PDF"
tell application "Adobe Illustrator" to open thisItem
tell application "Adobe Illustrator" to save current document in export_folder as pdf with options {class:PDF save options, PDF preset:"Low res proofing"}
tell application "Adobe Illustrator" to close current document
tell application "UltraLowPDF" to open
tell application "Finder" to move every file of folder "Macintosh HD:Users:j*****.*****:Desktop:AUTOMATOR:PDF" to container of this_folder
end open
This script should do a few things:
1. It creates a pdf file from an .ai file using adobe illustrator.
2. It then runs that pdf file through an automator workflow, which then spits out a pdf into a folder on my desktop.
3. It then moves that pdf file from the desktop, back to the folder of the original .ai file.
Every part of the script works fine except for step 3, it just seems to ignore it entirely. I've tried adding a delay, thinking the script was getting ahead of itself.
I've also tried isolating the move part of the script, using this droplet:
on open thisItem
set this_folder to (the POSIX path of thisItem)
tell application "Finder" to move every file of folder "Macintosh HD:Users:j****.******:Desktop:AUTOMATOR:PDF" to container of this_folder
end open
But this just throws up an error saying that it can't get the «class ctnr» of the file dropped on it (-1728).
Any help is greatly appreciated.
Thanks.
(note: I've starred out the user name part of the filepath for privacy)
EDIT: It seems that it's the Automator part of the script that is the problem, (the ''UltralowPDF"" app). After this runs, nothing after it in the script will run.
The problem is that thisItem is actually theseItems (aka a list). Either you need a repeat loop or – as in the following code – get the first item of the list.
The Finder does not accept (slash separated) POSIX paths. It works only with (colon separated) HFS paths.
The script uses the relative path path to desktop which point always to the desktop of the current user.
on open theseItems
set automatorFolder to (path to desktop as text) & "AUTOMATOR:"
set thisItem to item 1 of theseItems
set export_folder to automatorFolder & "export:"
set pdf_folder to automatorFolder & "PDF:"
tell application "Adobe Illustrator" to open thisItem
tell application "Adobe Illustrator" to save current document in export_folder as pdf with options {class:PDF save options, PDF preset:"Low res proofing"}
tell application "Adobe Illustrator" to close current document
tell application "UltraLowPDF" to open
tell application "Finder" to move every file of folder pdf_folder to container of thisItem
end open
I have created an Applescript that will, with one click, add the contents of a specific folder on my hard drive to iTunes (I really want to avoid using the iTunes organisational system).
Applescript
tell application "iTunes"
set theFolder to ("Macintosh HD:Temp to be Listened to:Temp on iPod:")
set with_subfolders to true --list subfolders or not (recursion)
set parent_folder to theFolder
end tell
tell application "System Events"
set item_list to parent_folder
end tell
tell application "iTunes"
add contents of parent_folder to user playlist "Temp on iPod"
end tell
However, it only imports into iTunes the files from the top level/parent folder.
I want to include files from the folders in the parent folder.
Is there a way I can get it to be recursive?
You do not need to get all files from a folder, since iTunes does this automatically and recursively from a folder.
Just add the parent folder, like this:
set parent_folder to "Macintosh HD:Temp to be Listened to:Temp on iPod:" as alias
tell application "iTunes"
add parent_folder to user playlist "Temp on iPod"
end tell
Solution #1: Using entire contents of the Finder - not very fast
set theFolder to "Macintosh HD:Temp to be Listened to:Temp on iPod:"
tell application "Finder" to set filesToAdd to files of entire contents of folder theFolder as alias list
tell application "iTunes" to add filesToAdd to user playlist "Temp on iPod"
Solution #2 : Using Spotlight - very fast, but the volume must be indexed.
set theFolder to "/Temp to be Listened to/Temp on iPod"
set fileList to paragraphs of (do shell script "mdfind -onlyin " & quoted form of theFolder & space & quoted form of "kMDItemContentTypeTree == '*public.audio*'")
set filesToAdd to {}
repeat with aFile in fileList
set end of filesToAdd to POSIX file aFile as alias
end repeat
tell application "iTunes" to add filesToAdd to user playlist "Temp on iPod"
My photo camera allows to save pictures in RAW and JPG in parallel. I find this convenient because on my Mac I can quickly browse the JPGs and delete the "bad" ones. Besides, I keep the RAW files of the "good" JPGs in case I need to do some deep editing.
I would like to write an AppleScript which deletes all the "bad" RAWs (RAW files which don't have a corresponding JPG anymore). All files are in the same directory.
This is my outline (far away from correct syntax!):
tell application "Finder"
set source_folder to choose folder with prompt "Please select directory."
my clearFiles(source_folder)
end tell
on clearFiles(source_folder)
set theItems to ""
tell application "System Events"
set theItems to get the name of every disk item of source_folder
end tell
repeat with theFile in theItems
if the extension of theFile is "raw" and exists name of theFile & ".jpg" then
tell finder
delete (name of theFile & ".raw") in source_folder
and tell
end if
end tell
end clearFiles
try this
set source_folder to choose folder with prompt "Please select directory."
tell application "Finder"
set rawFiles to every file of source_folder whose name extension is "raw"
repeat with aFile in rawFiles
set baseName to text 1 thru -5 of (get name of aFile)
set jpgFile to baseName & ".jpg"
if not (exists file jpgFile of source_folder) then delete aFile
end repeat
end tell