Copy multiple files to a folder in newbie appelscript - macos

I'm trying to make an Applescript that takes a list of names and makes a set of folders from it. And then copies files from a choosen folder into each of these newly created folders.
I have search for the answer, but didn't understand them. So if you could be so kind to explaine what and why I should change the code, it would be a great learning experience for me.
set destinationFolder to choose folder
set {text returned:textReturned} to display dialog "enter folder names" default answer return & return & return
if textReturned is "" then return
set folderWithFiles to (choose folder with prompt "Where are the files at?")
repeat with aLine in (get paragraphs of textReturned)
if contents of aLine is not "" then
do shell script "/bin/mkdir -p " & quoted form of (POSIX path of dest inationFolder & aLine)
set folderNew to aLine & ":" as alias
set dest to folderNew on destinationFolder -- as alias
tell application "Finder"
duplicate every file of folderWithFiles to dest
end tell
end if
end repeat
Thanks in advance
Updated: posted answer to myself

Got help from StefanK at macscripter.net
Here is the complete code:
set destinationFolder to choose folder
set {text returned:textReturned} to display dialog "enter folder names" default answer return & return & return
if textReturned is "" then return
try
set theFiles to (choose file with prompt "Where are the files at?" with multiple selections allowed)
end try
repeat with aLine in (get paragraphs of textReturned)
if contents of aLine is not "" then
set newFolder to (destinationFolder as text) & aLine
do shell script "/bin/mkdir -p " & quoted form of POSIX path of newFolder
try
tell application "Finder" to duplicate theFiles to folder newFolder
end try
end if
end repeat

Related

AppleScript: Download URL from file and keep the names

I want to create a simple AppleScript:
define a folder (that will get all the images, docs etc.)
read a file (.txt for example)
download all the URLs into the folder with their original name
Currently, my file has one URL by line, no separator.
I found several scripts but they always do something I don't want, don't need or doesn't work.
I found one that is very close to what I want, but it's editing the names in the loop.
I didn't succeed to split the URL and keep the last part.
Here it is:
property desktopPath : path to desktop as string
set n to 1
repeat with urlLine in paragraphs of (read alias (desktopPath & "filename.txt"))
set qURL to quoted form of urlLine
if qURL ≠ "''" then
set dest to quoted form of ¬
(POSIX path of desktopPath & "URLs/" & n & ".jpg")
do shell script "curl " & quoted form of urlLine & " > " & dest
set n to n + 1
end if
end repeat
If you have any other solution, I take.
Thanks in advance
I found a script that is working fine.
You just need to be sure about the txt file: the list inside should not have any text format.
Weirdly I have to do a copy/paste through a text editor to remove every text format.
1/ Put the txt file on your desktop, 2/ Create a "download" folder on your desktop, 3/ Start the script
set theList to (path to desktop as text) & "Download.txt" -- File name with the URLs
set theFolder to (path to desktop as text) & "download" -- Folder name between quotes
set theFolder to quoted form of POSIX path of theFolder
set theImages to read alias theList as list using delimiter linefeed
repeat with i from 1 to count of theImages
set thisItem to item i of theImages
do shell script "cd " & theFolder & "; " & "curl -O " & quoted form of thisItem
end repeat

Applescript, Automator - Find & Copy imgs in Dir/sub-dir

I am looking to make an automator workflow, or applescript (but I am not familiar with the language yet) that can use a list of names (spreadsheet or a .csv) to search a directory and its sub-directories for those specific file names (with varying extensions) and copy that image to a folder created for these images.
I've found a script that seems to be somewhat similar to what I need, but it doesn't search within sub-directories, so I've yet to actually find any images with it.
After extensive research, I've found 2 different scripts that seem to be what I need, but neither of them seem to search sub-directories. Below are the 2 scripts I've tried. If anyone could help me get these to search sub-directories, I would really appreciate it!
Script 1:
set thePhotos to paragraphs of (read (choose file with prompt "Choose a text file"))
set theSourceFolder to (choose folder with prompt "Choose source folder")
set theDestination to (choose folder with prompt "Choose destination folder")
set dupeList to {}
repeat with theName in thePhotos
try
set end of dupeList to alias ((theSourceFolder as text) & theName)
end try
end repeat
tell application "Finder" to duplicate dupeList to theDestination with replacing
set theCount1 to (count of dupeList) as text
set theCount2 to (count of thePhotos) as text
display dialog (theCount1 & " of " & theCount2 & " items copied to " & (theDestination as text)) buttons {"OK"}
Script 2
set fileContents to read (choose file with prompt "Choose a comma-delimited text file")
set theText to result
set AppleScript's text item delimiters to ","
set theTextItems to text items of theText
set AppleScript's text item delimiters to {""}
theTextItems
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theEPSName in theTextItems
tell application "Finder"
set theEPSFile to theSourceFolder & theEPSName
move file theEPSFile to folder theDestination with replacing
end tell
end repeat
So it lets me choose the .csv to use, choose the directory to save and choose the directory to search. How can I get this script to search within sub-directories? And to someone who understands Applescript, does this look like it will function as needed?
Thank you in advance! I really appreciate any help on this, as it is my first Applescript experience, but I am excited to learn it!
This is a very fast solution using the shell and Spotlight searching.
The script creates a search criteria string
'kMDItemFSName == picture1.png || kMDItemFSName == picture2.jpg || ...'
then performs the search and copies all files by passing the found items to the cp command.
There might be a caveat if the name list is too long and exceeds the maximum length of shell parameters.
set theNames to paragraphs of (read (choose file with prompt "Choose a text file" of type "txt"))
set sourceFolder to quoted form of POSIX path of (choose folder with prompt "Choose source folder")
set destinationFolder to quoted form of POSIX path of (choose folder with prompt "Choose destination folder")
set nameList to {}
repeat with aName in theNames
set end of nameList to "kMDItemFSName == " & quoted form of aName
end repeat
set {TID, text item delimiters} to {text item delimiters, " || "}
set nameFilter to quoted form of (nameList as text)
set text item delimiters to TID
do shell script "mdfind -onlyin " & sourceFolder & " -0 " & nameFilter & " | xargs -0 -J {} cp {} " & destinationFolder

(Automator/AppleScript) Rename files to a folder name, save to different folder & add prefix

I really need you help and will be very grateful for it.
I think this should be no problem for you.
So I have folder with different subfolders in it. In subfolders are images.
What I need is:
Change each image name to it’s subfolder name + 001 and so on («1stSubfolder001, 1stSubfolder002, …», «2ndSubfolder001, 2ndSubfolder002, …»)
Move all images from all subfolders to one folder (or at least to root folder)
Add random prefix number to names.
I have script to third task:
tell application "Finder"
repeat with this_item in (get items of window 1)
set name of this_item to ((random number from 0 to 99999) & name of this_item) as string
end repeat
end tell
But it only works with opened folder on the foreground. Maybe there is a way to make one script to run it on the background?
Huge thank you in advance.
I found cool automator app here, but I need to correct it a little bit.
How do I use the current folder name without a path as a variable in automator in Mac?
This script does everything I need BUT it renames all files to ONE folder name, not each different.
Here is another applescript that does rename files to folders, but it uses 2 folders name (folder + subfolder), and I need only subfolder prefix.
set myFolder to do shell script "sed 's/\\/$//' <<< " & quoted form of POSIX path of (choose folder)
set myFiles to paragraphs of (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -maxdepth 2 -mindepth 2")
repeat with aFile in myFiles
tell application "System Events" to set file aFile's name to (do shell script "sed 's/.*\\/\\([^/]*\\)\\/\\([^/]*\\)\\/\\([^/]*$\\)/\\1_\\2_\\3/' <<< " & quoted form of aFile)
end repeat
(MacBook Pro Late 2013, OS X Yosemite 10.10.1)
This accomplishes what you want. You should be able to adjust from here. There are no checks to ignore non-image files.
property top_folder : alias "Macintosh HD:Users:MyName:Downloads:Images:"
property save_folder : ""
set save_folder to choose folder with prompt "Select the folder to save the images in."
process_folder(top_folder)
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
set container_name to name of (info for this_folder)
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
if folder of (info for this_item) is true then
process_folder(this_item)
else
process_item(this_item, container_name, i)
end if
end repeat
end process_folder
-- this sub-routine processes files
on process_item(this_item, c, i)
-- make the integer a 3 digit string
if i < 10 then
set i to "00" & i
else if i < 100 then
set i to "0" & i
end if
-- set a random number
set r to (random number from 0 to 99999) as string
tell application "System Events"
-- get file extension so not overwritten
set e to name extension of this_item
set new_name to "" & r & "_" & c & "_" & i & "." & e
set name of this_item to new_name
move this_item to save_folder
end tell
end process_item

Open Finder alias

I'm relatively new to Applescript, I was hoping someone can help me resolve this one...
I have a script that does a spotlight search and returns the found items as founditems. The result will either be a folder or an alias to a folder. I'd like to open the found item and it works if the result is a folder but I can't figure how to handle an alias. With an alias I get the error incorporated in the code
try
set theapp to default application of (get info for (POSIX file founditems)) as string
tell application theapp to open (POSIX file founditems as string)
activate application theapp
on error e
display dialog "An error has occured trying to open your file:" & return & return & e buttons {"OK"} default button 1
end try
I get 10665 error code. My guess is that incorporating original path may resolve the alias issue but I'm not sure how to plug it in... Thanks much
founditems is created this way:
set input_var to "12345"
set spotlightquery to "\"kMDItemFinderComment == '" & input_var & "'\""
set thefolders to {POSIX file "/Volumes/RAIDvolume"}
set founditems to {}
repeat with i in thefolders
set thepath to quoted form of POSIX path of i
if exists thepath then
set command to "mdfind -onlyin " & thepath & " " & spotlightquery
set founditems to founditems & (paragraphs of (do shell script command))
end if
end repeat
So it was a list because you used "paragraphs of" and I can see now that founditems is a list of posix paths. As such the following will open it with the default application regardless if the path is to a file, folder or alias.
set founditems to {"/Users/hmcshane/Desktop/aaa alias"}
set macPath to POSIX file (item 1 of founditems)
tell application "Finder" to open macPath

Applescript - move files to folder which name begins with first 2 characters of filename

I'd like a little help fine tuning this script. Is it possible to have this script match subfolders based on the first two letters of file? Parent folder would be "Shots" which contains subfolders with unique two letter prefix "BA_Bikini_Atol" which contains subfolders within those folders for the specific shots ba_0020, ba_0030, etc. Would like to move a file, ba_0020_v0002 to the ba_0020 folder by selecting "shots" as the target and the script looks through all the sub folders for a match. Thoughts?
set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")
tell application "System Events"
set folderList to name of folders of mgDestFolder
set fileList to name of files of mgFilesFolder
end tell
repeat with i from 1 to (count folderList)
set folderName to item i of folderList
set filesToMove to {}
repeat with j from 1 to (count fileList)
set filename to item j of fileList
if filename begins with folderName then
set end of filesToMove to alias ((mgFilesFolder as string) & filename)
end if
end repeat
tell application "Finder"
move filesToMove to alias ((mgDestFolder as string) & folderName & ":")
end tell
end repeat
This works in my tests
set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")
(* get the files of the mgFilesFolder folder *)
tell application "Finder" to set fileList to files of mgFilesFolder
(* iterate over every file *)
repeat with i from 1 to number of items in fileList
set this_item to item i of fileList
set this_file_Name to name of this_item as string
(* get the file name code
Using the delimiter "_" break the name into fields of text and returning fields 1 thru 2 . i.e ba_0030 *)
set thisFileCode to (do shell script "echo " & quoted form of this_file_Name & " |cut -d _ -f 1-2")
log thisFileCode
tell application "Finder"
set folderList to ((folders of entire contents of mgDestFolder) whose name starts with thisFileCode)
if folderList is not {} then
move this_item to item 1 of folderList
end if
end tell
end repeat
FROM:
TO:
I've made (with the help from users of this site) the script you're referring to.
I've made similar (more efficient) scripts since then. Most of them depend on ASObjC Runner which you can get here: Download ASObjC Runner
If you have it installed I think the following script will work:
set mgFilesFolder to (choose folder with prompt "Where are the file stored which you would like to move?")
set mgDestFolder to (choose folder with prompt "Where are destination folders?")
tell application "ASObjC Runner"
set mgFiles to enumerate folder mgFilesFolder with recursion without including folders
repeat with mgFile in mgFiles
set mgPrefix to text 1 thru 7 of name of (about file mgFile include only "name")
set mgDest to enumerate folder mgDestFolder match prefix mgPrefix with recursion
manage file mgFile moving into mgDest
end repeat
end tell

Resources