I am trying to write an AppleScript that iterates through the folders in my Desktop folder and moves them to a subfolder called "destination". (I need to iterate through the Desktop folders instead of moving them all at once because I want each folder to be moved or not moved based on a randomly generated number).
My code:
set desktopFolders to (path to desktop as text)
set destinationFolder to (path to desktop as text) & "destination"
tell application "System Events"
set subFolders to (get every disk item of folder desktopFolders)
repeat with eachFolder in subFolders
move eachFolder to destinationFolder
end repeat
end tell
The script works if I try to move the entire subFolders variable into destinationFolder, and it doesn't throw an error from trying to iterate through eachFolder in subFolders. However, when I try to move eachFolder into destinationFolder, I get the following error:
System Events got an error: NSArgumentsWrongScriptError
Any reason the eachFolder variable isn't able to be moved in this way? Is there a better way to iterate through the folders in my Desktop folder, such that I can move each one of them individually?
Change desktopFolders to: desktopFolder
Note there is no s on the end, as the Desktop folder is singular not plural.
Change disk item in:
set subFolders to (get every disk item of folder desktopFolder)
To: folder
Also add:
whose name is not "destination"
To the end of:
get every folder of folder desktopFolder
Example:
set subFolders to (get every folder of folder desktopFolder whose name is not "destination")
You do not want to try moving the destination folder into itself.
Sans making desktopFolders singular, these other changes allow the rest of the code to work without error while testing under macOS High Sierra.
Related
I would like to create an Applescript that mimics the behavior of an alias to a specific folder X. If files are dragged to it, they should be moved to folder X, but if the Applescript is just double-clicked, folder X should be opened.
I am an Applescript novice, but adapting things I've found on the web, I have constructed working pieces: one that moves dragged files to X and one that opens X. I do not know enough to combine them with "if...else" logic, so that if nothing is dragged, the folder is opened; but if things are dragged, they are moved to X but X is not opened.
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
tell application "Finder"
set folderMyFolder to folder "/Users/myname/myfolder"
move theCurrentDroppedItem to folderMyFolder
end tell
end repeat
end open
tell application "Finder" to open "Macintosh HD:Users:myname:myfolder"
When your script contains an open handler, it will be passed items dropped onto the application, while the run handler is the one called when the application is double-clicked. You don't need to do any comparisons, just place the statements to be performed in the appropriate handler, for example:
property targetFolder : ((path to home folder) as text) & "path:to:myfolder"
on run
tell application "Finder" to open folder targetFolder
end run
on open droppedItems
repeat with anItem in droppedItems
tell application "Finder" to move anItem to folder targetFolder
end repeat
end open
Note that the Finder doesn't know about POSIX paths, so to use them you will need to coerce the paths to something the Finder can handle, or use something that does know POSIX paths, such as System Events.
I created a droplet which would allow me to rename files from an input box.
on adding folder items to este_folder after receiving este_file
display dialog "what's their name?" default answer ""
set text_returned to text returned of the result & ".jpg"
display dialog text_returned
tell application "Finder"
set the name of file este_file to text_returned
end tell
end adding folder items to
It works fine, but it creates a loop where I have to hit cancel again to stop the script, as it thinks a new file has been added. I would like to just rename it once; and then not have the second dialog box pop up again. I have tried rerouting the file to another folder:
on adding folder items to este_folder after receiving este_file
display dialog "what's their name?" default answer ""
set text_returned to text returned of the result & ".jpg"
display dialog text_returned
tell application "Finder"
set the name of file este_file to text_returned
end tell
repeat with anItem in este_file
tell application "Finder"
set destFolder to "Macintosh HD:Users:maxwellanderson:Desktop:BetterinTexas" as alias
move anItem to folder destFolder
end tell
end repeat
end adding folder items to
But that doesn't work either-it doesn't process the renaming portion of the script. Any suggestions on what I should do to get rid of the second dialog box?
The script is being called twice because renaming the file within the watched folder is—for all intents and purposes—like adding a new file into the folder. Therefore, it's called once when the file is actually added; and called a second time when it's renamed.
Moving the file as you suggested will work, but you have to move the file before you rename it. Therefore, shove the code that moves the file near the top of the script, then the renaming bits at the bottom.
As a side-note, I notice you have a repeat with loop to handle multiple file moves, but only one statement that handles a single file renaming. One of these is not like the other. If this watched folder received multiple files at the same time, it would most likely rename them all to the same name, thus potentially over-writing multiple files. If the watched folder only ever receives one file at a time, anyway, then the repeat with loop is redundant.
This code is modelled on yours and will handle a single file move-and-rename (but not a group of files—or, more accurately, as I stated above, it would rename multiple files to the same name, thus overwriting all but the last one in the list):
on adding folder items to este_folder after receiving este_file
set destFolder to POSIX file "/Users/maxwellanderson/Desktop/BetterinTexas" as alias
set text_returned to text returned of ¬
(display dialog "what's their name?" default answer "") ¬
& ".jpg"
display dialog text_returned
tell application "Finder" to ¬
set the name of ¬
(move file este_file to destFolder) ¬
to text_returned
end adding folder items to
If you need it to handle multiple files, then you can wrap everything from set text_returned to to text_returned in a repeat with loop as you've done in your second code block. This will sequentially bring up dialog boxes—one per file—and move/rename the files accordingly.
If you have any questions, or need clarification, leave a comment and I'll get back to you.
I found many topics on that but none of the solutions seem to fit what i want to do or they don't work.
I want to select multiple images in a folder in a finder window.
I want to right-click the selection and run a Service to create a PDF using those images in the same folder those images are in.
I don't know how to get the directory the images are placed in.
Assuming that your selected file could be from different folders, you must loop to each image and get parent folder as bellow :
tell application "Finder"
set myFIles to selection -- read your selection of images
repeat with aFile in myFIles -- loop for each selecgted image
set ParentFolder to container of aFile -- get folder which contains that image
end repeat
end tell
I'm new to applescript but I want to set up a folder action that:
1. Recognises when a file is added to a folder
2. Tags said folder red
3. Adds a Reminder to the "Downloads" reminder list that has the name of the newly-added file as the body text of the reminder
Using google and Applescript's record function I've frankenscripted this together so far
property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.
on adding folder items to this_folder after receiving added_items
try
tell application "Finder"
set FILEname to name of (added_items)
set label index of folder "untitled folder" of folder "Desktop" of folder "heyjeremyoates" of folder "Users" of startup disk to 2
end tell
tell application "Reminders"
set mylist to list "Downloads"
tell mylist
make new reminder with properties {name:"D/L Complete", body:FILEname, due date:(current date)}
end tell
end tell
end try
end adding folder items to
Darn thing won't work. Infuriating. I tested it as a folder action with "test" as the name and body of the reminder and it worked fine. I'm pretty sure I've gone wrong somewhere in setting FILEname as the name of the newly copied item because the script as it is now no longer turns the folder red.
The idea behind this is so I can see, from my iPhone/iPad, how many large/scheduled downloads to my home mac (both torrents and large work files - I'll have a seperate folder action and reminder list for each download folder) there are that are yet to be managed.
It seemed like setting up a Growl/Prowl combo was wasteful if iCloud/Reminders and a dozen lines of code could deliver what I wanted anyway. Ideally I'll write a second applescript that will delete the reminder when I rename or move the related file, and though I haven't even thought about how that would work if anyone has any suggestions for it I'd be super grateful
It is a pity you can't (natively) have OSX notifications pushed to an iOS device linked to the same iCloud account though (with the appropriate granularity)
But I digress - can anyone see what I'm screwing up here?
Thanks in advance for even reading this far
added_items is a list of aliases, and name of (added_items) resulted in an error.
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set label index of this_folder to 2
repeat with f in added_items
set n to name of f
tell application "Reminders" to tell list "Downloads"
make new reminder with properties {name:"D/L Complete", body:n, due date:(current date)}
end tell
end repeat
end tell
end adding folder items to
(Save the script in ~/Library/Workflows/Applications/Folder Actions/ and enable the folder action from Folder Actions Setup.)
I store photo files in folders on my computer before i add them into iphoto.
I want to add the contents of a selected to folder to iphoto and name the new album as the folder name
I have created an automator flow where I do the following
(Select folder initially to work)
Get selected finder items -- This is grabbing the folder
Set Value of Variable -- this is grabbing the full path name and setting a variable with the name
Get Folder contents -- this gets all the photos contained.
Import Files into iphoto -- This adds the photos into iphoto and creates a new album using the variable name.
The issue i have is the variable name sets the full path of the files,
/Users/Johnny/Photos/Dayout
Is the a script that can take just the name of the initial folder "Dayout"
Thanks in advance to anyone who can help
Cheers
John
activate application "SystemUIServer" -- http://www.openradar.me/9406282
tell application "Finder"
activate
repeat with f in (get selection as alias list)
set n to name of f
tell application "iPhoto"
if not (exists album n) then new album name n
import from f to album n
end tell
end repeat
end tell
There is a bug in 10.7 and 10.8 where Finder ignores new windows when getting the selection property. If you open a new Finder window, select some items, and run tell app "Finder" to selection in AppleScript Editor, the result is items selected in some window behind the frontmost window (or an empty list). One workaround is to move focus to another application and back.