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"
Related
I can copy the file using AppleScript.
But how can I use it to paste the copied file to another folder?
I couldn't find the right operator or similar question.
tell application "Finder" to set the clipboard to (POSIX file "/Users/bgbg/Library/Messages/chat.db")
It definitely seems odd to me to use the clipboard as the variable for copying and moving files or folders and to use the keystroke command to paste a file into a folder. However, if you are hell-bent on doing things this way, I do believe this following AppleScript code will accomplish what you’re looking to achieve
-- Set The Clipboard To A File (This Only Works With A Single File¬
-- Or Folder And Not With A List Of Files Or Folders)
-- set the clipboard to POSIX path of (choose file with prompt ¬
-- "Set The Clipboard To A Single File Or Folder" invisibles false ¬
-- without multiple selections allowed) as POSIX file
set the clipboard to POSIX path of (((path to library folder from user domain as text) ¬
& "Messages:chat.db") as alias) as POSIX file
set destinationFolder to (choose folder with prompt ¬
"Choose Your Destinations Folder" with invisibles)
tell application "Finder"
activate
repeat until frontmost
delay 0.1
end repeat
reveal destinationFolder
end tell
delay 1
tell application "System Events" to keystroke "v" using {command down}
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
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
I've been trying to get System Events to duplicate files in AppleScript and I've been failing :) I eventually always get the error "error "Files can not be copied." number -1717". So I changed my tactics and tried using the Finder to make sure what i was trying to do was correct. Here is the code that works:
tell application "System Events"
set desktopFolder to (path to desktop folder) as string
set fullPath to desktopFolder & "Temp Export From DO"
set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
set source to path of DOEntry
log "Source file: " & source
set destination to fullPath as string
log "Destination folder: " & destination
tell application "Finder"
duplicate file source to folder destination with replacing
end tell
end repeat
end tell
If I remove that last tell, so that it uses System Events, I get the same error noted above. The dictionary for System Events standard suite has a "duplicate" command so I'm not sure what is going on here. Also, "Learning AppleScript, 3rd ed" from APress notes:
"One particularly annoying omission in System Events is that it can’t yet duplicate files and folders; if you need to do this, the Finder is your best bet."
The 3rd edition is from 2010. It would seem that even in Mountain Lion this is still true. Can anyone confirm this? The 1717 error number lists everywhere else as a handler error and i'm not using handlers.
Unfortunately, you cannot duplicate files using System Events - you have to use the Finder. Even in the answer provided by adayzdone, System Events is not actually handling the duplication.
This looks like it's working (because it's inside a System Events tell block)...
tell application "System Events"
duplicate myFile to myFolder
end tell
...but if you inspect the event log you'll see that the Finder is actually performing the duplication. Behind the scenes, you are passing two Finder objects to System Events. System Events doesn't know how to handle Finder objects, so execution is passed to the objects' owner, the Finder, which executes the command.
For file duplication in AppleScript, you are unfortunately limited to using the Finder or the command line via do shell script.
Try:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
repeat with DOEntry in theDOEntries
log "Source file: " & DOEntry
log "Destination folder: " & desktopFolder
tell application "Finder" to duplicate file DOEntry to desktopFolder with replacing
end repeat
If you don't need to log the values you can simply:
tell application "Finder" to set desktopFolder to (path to desktop folder as text) & "Temp Export From DO" as alias
tell application "System Events" to set theDOEntries to every file of folder "/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries" whose name extension is "doentry"
tell application "Finder" to duplicate theDOEntries to desktopFolder with replacing
Or:
set desktopFolder to quoted form of (POSIX path of (path to desktop folder as text) & "Temp Export From DO")
do shell script "find '/Users/jkratz/Dropbox/Apps/Day One/Journal.dayone/entries' -name \"*.doentry\" -type f -print0 | xargs -0 -I {} cp -a {} " & desktopFolder
Getting back to your question, the duplicate commands creates duplicates of Finder items. You can use System Events to duplicate Finder items like this:
tell application "Finder"
set myFile to file ((path to desktop as text) & "Test File.txt")
set myFolder to folder ((path to desktop as text) & "Test Folder")
end tell
tell application "System Events"
duplicate myFile to myFolder
end tell
on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
return input
end run
Is what I have so far, and it works, but is there a way to then focus on the file then trigger a rename? I dont want the rename to be automatic, just trigger the event (like pressing "return" while you have a file selected). And I dont want to use any sort of modal...
Quick Side question: is there a way to set this so that i dont have to select a folder or file directly, but can do it by, lets say, clicking in a white space in a folder as long as it's in Finder? Right now I have my "Service receives selected" to "files or folders" in Finder.app.
== UPDATED CODE ==
on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
tell application "Finder"
activate
set target of Finder window 1 to POSIX file "/Users/oscargodson/Documents/designs/untitled"
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
return input
end run
If i hardcode the path it works! But how do I get it as a var that works?
Here's one way. I think a modal window where you ask for the name would be better but you can try this. Notice you do not use "POSIX path" in this code. Applescript doesn't use POSIX paths. Also {input}, as indicated by the brackets around it, is a list of items. Therefore you act on the items of the list, and in this case we act on the first item.
set filepath to item 1 of input
tell application "Finder"
activate
reveal filepath
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
EDIT: With your updated code, here's a working script...
on run {input}
if (class of input) is not list then set input to {input}
set theFolder to (item 1 of input) as text
try
alias theFolder
tell application "Finder"
if (class of item theFolder) is not folder then error "input is not a folder."
activate
set theFile to make new file at folder theFolder with properties {name:"untitled"}
reveal theFile
end tell
delay 0.2
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
on error theError number errorNumber
tell me
activate
display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
return
end tell
end try
return input
end run
tell application "Finder"
activate
reopen -- in case there are no open windows
set target of Finder window 1 to POSIX file "/Applications/Safari.app"
end tell
reveal and select always open a new window, set target and set selection don't.
I don't know why, but when set selection it used in column view, you can only select items in the entire contents of the target of the front window. The same thing doesn't happen in other views, so it seems like a bug.
Fix for the code in the edited question:
on go(input)
set p to POSIX path of (input as text)
set p2 to p & "untitled"
do shell script "touch " & p2
tell application "Finder"
reopen
activate
set target of Finder window 1 to POSIX file p2
end tell
delay 0.3 -- time to release modifier keys
tell application "System Events" to keystroke return
end go
tell application "Finder"
set fold to folder (path to documents folder)
end tell
go(fold)
(That on go and the last lines are just for testing.)
I've created an AppleScript based on the #regulus6633's one, but with some improvements.
Note: This answer was originally posted as an AskDifferent answer. I'm copy/pasting here for convenience.
The idea is to create an Automator workflow and assigning a shortcut to it using the following steps:
Open Automator and create a Service;
Set the input to no input, and the application to Finder.app;
Drag and Drop the Run AppleScript workflow element onto the grey space;
Put the contents of this AppleScript in the textbox;
Save the workflow with a reasonable name (like New File);
Go to Settings -> Keyboard -> Shortcuts -> Services and assign a shortcut to it.
Now, let's show the AppleScript:
set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false
-- get folder path and if we are in desktop (no folder opened)
try
tell application "Finder"
set this_folder to (folder of the front Finder window) as alias
end tell
on error
-- no open folder windows
set this_folder to path to desktop folder as alias
set is_desktop to true
end try
-- get the new file name (do not override an already existing file)
tell application "System Events"
set file_list to get the name of every disk item of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
if new_file is in file_list then
set new_file to file_name & " " & x & file_ext
set x to x + 1
else
exit repeat
end if
end repeat
-- create and select the new file
tell application "Finder"
activate
set the_file to make new file at folder this_folder with properties {name:new_file}
if is_desktop is false then
reveal the_file
else
select window of desktop
set selection to the_file
delay 0.1
end if
end tell
-- press enter (rename)
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
For convenience, I'm putting this AppleScript in this GitHub Gist.