Concatenate POSIX file paths from a list - applescript

I have a list with folder paths who should concatenate correctly to have multiple sourcefolders backed up into a single source folder with the rsync command.
This is what I have so far:
set sourcefolderlist to {"/Volumes/sourcefolder1", "/Volumes/sourcefolder2"}
set localfolder to quoted form of POSIX path of ("/Users/dfdfdf/destinationfolder")
set allSourceFolders to ""
repeat with oneSourceFolder in sourcefolderlist
set allSourceFolders to ((quoted form of POSIX path of allSourceFolders) & oneSourceFolder)
end repeat
so the source-paths should be in a list which concatenate with POSIX file paths (like localfolder).
The end should look this:
do shell script "rsync -arvuE " & allSourceFolders & " " & localfolder
How can I concatenate the items in sourcefolderlist so that rsync can read the correct POSIX folder paths?

From the man page:
rsync [OPTION]... SRC [SRC]... DEST
I think you just separate them with a " " character.
If you still need a list as source of all sources (comes in handy), loop thru it like this:
set sourcefolderlist to {"/Volumes/sourcefolder1", "/Volumes/sourcefolder2"}
set localfolder to quoted form of POSIX path of ("/Users/dfdfdf/destinationfolder")
set sources to ""
repeat with i from 1 to number of items in sourcefolderlist
set sources to sources & " " & quoted form of (item i of sourcefolderlist as text)
end repeat
log sources -- > (* '/Volumes/sourcefolder1' '/Volumes/sourcefolder2'*)
do shell script "rsync -arvuE " & sources & " " & localfolder
BTW: use AppleScript's built-in code snippets: make a new line, paste "sourcefolderlist" (=the variable containing the list) into the new line and CTRL-Click it. Select "Repeat Routines" and from there "Process every item".

I wrote a Applescript Script library ( Join list items v2 ) to help with joining Applescript list items in which you can also add quotes to the items as they are placed in the final string.
Example of use in this case would be:
#Needed Use Clauses
use script "you Library name here"
--use scripting additions
set sourcefolderlist to {"/Volumes/sourcefolder1", "/Volumes/sourcefolder2"}
set sources to join list items sourcefolderlist using text space with items in single quotes
-- Result--> "'/Volumes/sourcefolder1' '/Volumes/sourcefolder2'"
Also a small explanation about the library (v1) in the post for version 1. Which also has links to Apples sessions on Applescript Script Libraries

Related

Check file name for range of characters

Is there a way to tell Applescript to locate a file where the file name would match this type of criteria:
set foundFile to file whose name starts with
[character 0-9] followed by [character 0-9] followed by underscore
I can't think of a way to do it with a Finder whose clause statement. However grep has regular expression capabilities so try this...
set theFolder to choose folder
set foundFileNames to paragraphs of (do shell script "ls " & quoted form of POSIX path of theFolder & " | grep '^[0-9][0-9]_'")
set firstFoundFile to (theFolder as text) & item 1 of foundFileNames
Here's a handler that does this with all Applescript and no shell script.
-- get your folder however and feed the folder to the handler getMatchingFiles(*your folder*)
on getMatchingFiles(theFolder)
tell application "Finder"
set theFiles to every file of theFolder
set allDigits to "0123456789"
set matchingFiles to {}
repeat with aFile in theFiles
set fileName to the name of aFile
if ((the first character of fileName is in allDigits) and (the second character of fileName is in allDigits) and (the third character of fileName is "_")) then copy aFile to the end of matchingFiles
end repeat
end tell
return matchingFiles
end getMatchingFiles
regulus6633's answer is promising, but there's no need to resort to resort to external utilities ls and grep; the shell's own pathname expansion (globbing) is sufficient, using pattern [0-9][0-9]_*:
# Pick a folder.
set targetFolder to choose folder
# Get matching files.
# `shopt -s nullglob` instructs the shell to return an empty string, if no files match.
set foundFiles to paragraphs of (do shell script "shopt -s nullglob; printf '%s
' " & quoted form of POSIX path of targetFolder & "[0-9][0-9]_*")
# Extract the 1st matching file.
set foundFile to item 1 of foundFiles

Using multiple rsync jobs in applescript

Hello I want to use AppleScript to copy a file from a source Macserver to 10 other Mac servers using rsync. I have the basics working:
set source to "/Folder1/Folder2/"
mount volume "afp://username:password#server1/Folder1/"
set Folder1 to result as alias
set destShareName to "/Volumes/Folder1/Folder3"
do shell script "/usr/bin/rsync -rlptD --log-file=/Users/user/Documents/rsync.txt " & (quoted form of source) & " " & (quoted form of destShareName)
It works and I get a log of the job. I know I could copy the job and substitute server1 with server2 and run the job again. How can I create a list of servers and get the rsync job to run recusively through the list? Many thanks, John
Something like this. You make lists and use a repeat loop. The first time through the repeat loop you grab all the first items in the lists, so make sure all the first items in each list go together. The second time through you will grab all the second items and so on. Note that all the lists should have exactly the same number of items.
set theServers to {"afp://username:password#server1/Folder1/", "afp://username:password#server2/Folder1/"}
set sourceFolders to {"/source1Folder/Folder2/", "/source2Folder/Folder2/"}
set destFolders to {"/dest1Folder/Folder2/", "/dest2Folder/Folder2/"}
repeat with i from 1 to count of sourceFolders
set thisServer to item i of theServers
set thisSource to item i of sourceFolders
set thisDest to item i of destFolders
mount volume thisServer
do shell script "/usr/bin/rsync -rlptD --log-file=/Users/user/Documents/rsync.txt " & (quoted form of thisSource) & " " & (quoted form of thisDest)
end repeat
EDIT: based on your comment, if only your server changes then you could adjust your code like this...
set theServers to {"afp://username:password#server1/Folder1/", "afp://username:password#server2/Folder1/"}
set sourceFolder to "/source1Folder/Folder2/"
set destFolder to "/dest1Folder/Folder2/"
repeat with i from 1 to count of theServers
mount volume (item i of theServers)
do shell script "/usr/bin/rsync -rlptD --log-file=/Users/user/Documents/rsync.txt " & (quoted form of sourceFolder) & " " & (quoted form of destFolder)
end repeat

Applescript - Getting a section of a POSIX path and creating a folder with that name

I have some files in a work folder that contains jobnames_jobnumber /work/jobnameA_001/outbox/filename.mp4, /work/jobnameB_002/outbox/flamename.mp4 e.t.c
I would like to have an applescript get the full POSIX path and extract just the jobnameA_001 part of the path then create a new folder with jobnames_jobnumber_date
Please note the jobnames are client names so they are not fixed length.
Please also note I am a complete newbie and am not sure what to research to find the answers. But i'll try.
Thank you kindly,
Here's code that does what you want:
# Extract all <jobName>_<jobNumber> tokens from matching paths.
set jobNamesWithNumbers to paragraphs of ¬
(do shell script "for p in /work/*/outbox/*.mp4; do echo \"$p\"; done |
awk -F/ '{ print $2 }'")
# Get the date in format YYYY-MM-DD, e.g., "2014-04-13".
set dateString to do shell script "date +'%Y-%m-%d'"
# Specify the target folder in which to create the new folders:
set targetFolder to POSIX path of (path to desktop)
# Build the absolute paths of the folders to create.
set quotedFolderPathList to ""
repeat with itm in jobNamesWithNumbers
set folderPath to (targetFolder & "/" & itm & "_" & dateString)
set quotedFolderPathList to quotedFolderPathList & " " & ¬
quoted form of folderPath
end repeat
# Finally, create the new folders.
do shell script "mkdir -p" & quotedFolderPathList
Note that this relies heavily on using shell (bash) commands invoked with do shell script, which makes for much shorter code.
This comes at the cost of having to understand two languages, however.
Given AppleScript's limitations, it's worth making this learning investment in the long run.

AppleScript to read text file containing file names, search for each file name, and if a file is found copy it to a folder

Sorry to ask a question without even pasting my coding attempt, but I've never used AppleScript before and I have no idea how I would do this. I've found bits of code online that do small parts of each step of this, but some of the key parts I can't figure out how to do. If I can get this figured out it would save a lot of time. Basically my problem is that a client sent over thousands of photos, all in multiple levels of sub folders, along with an Excel document containing about 300 file names that I need to pull out and use. I can copy the file names from the Excel document into a plain text file, either multi-line or comma separated.
So this is what I need to do:
Open folder selector dialog to select the destination folder
Open file selector dialog to select the text file
Loop through each line (or comma separated value) of the text file
Take that string and search for a file name containing the string
Copy the first result into a folder (let's say Desktop:Found Photos)
If a file could not be found matching the string then add the search string into a text file (so I could email it to the client and ask them to send it to me)
If you can't code this whole process, if you could help me with looping through the text file, searching for the file name and copying the first result to another folder, and adding the file name to a text file if a file wasn't found, then I could probably piece it it all together. Thanks for any help.
You can try something along the lines of:
set newFolder to POSIX path of (path to desktop as text) & "Found Photos"
do shell script "mkdir -p " & quoted form of newFolder
set filePaths to paragraphs of (read (choose file with prompt "Select file list") as «class utf8»)
set fileFolder to POSIX path of (choose folder with prompt "Select folder containing files")
set foundFiles to {}
repeat with fileName in filePaths
set fileName to (contents of fileName)
set xxx to do shell script "find " & quoted form of fileFolder & " -name " & quoted form of fileName
if xxx ≠ "" then
tell application "System Events" to move file xxx to newFolder
set end of foundFiles to fileName & return
end if
end repeat
set foundFiles to (foundFiles as text)
do shell script "echo " & quoted form of foundFiles & " > " & quoted form of POSIX path of ((path to desktop as text) & "FoundFiles.txt")
It might have been easier to use shell scripting:
IFS=$'\n'
mkdir -p ~/Desktop/target/
for l in $(cat ~/Desktop/files.txt); do
found=$(find ~/Documents/source -type f -name "*$l*")
[[ -n $found ]] && cp $found ~/Desktop/target/ || echo "$l"
done

Moving files into folders / subfolders based on file names [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Hope someone can help me,
To start out, my AppleScript skills are almost non-existing.
I get a number of files for archiving purposes in the following format which need sorting. The format is first the device name, followed by a run number and a sample number followed by some text.
Example file: "UZLA 55879 [05x13] september cal run.cdf" (file format varies)
Which needs to be moved into a folder: ~/UZLA 55879 (LCMS)/Run 5/
The device name is fairly random, sometimes just a number sometimes the entire official naming.
The main folder has a secondary item in brackets after the device name which is not in the file name that is being moved. the string before "[" and "(" do match, after the main name it's different.
The subfolder doesn't always exist, when a new run is started the folder /Run 6/ for example might not exist. Their's no 0 padding to the numbers
The files all arrive in the same folder and their should be no other files located in that folder.
To round this of we like to make an alias in a single folder on the main drive (files are moved to external system) for direct access, which is easy for quick last think look up but entirely unwieldy for the whole system (older aliases are deleted from it by other script).
Thanks.
Okay, so this was annoyingly closed, appearance because it only helps me and not random person from the future. Getting help for me was kinda the point. This is where we're at, with thanks to adayzdone:
set myFolder to (choose folder)
tell application "System Events" to set folderName to myFolder's name
set {TID, text item delimiters} to {text item delimiters, {"(", ")"}}
set myText to text item 2 of folderName
set text item delimiters to TID
set myFiles to every paragraph of (do shell script "find " & quoted form of (POSIX path of myFolder) & " \\! -name \".*\" -type f")
repeat with aFile in myFiles
set aFile to aFile as text
tell application "Finder" to set fileName to name of (POSIX file aFile as alias)
set newPath to "~/" & quoted form of (do shell script "echo " & quoted form of fileName & " | sed -e 's/\\[0/\\[/' -e 's/\\([^\\[]*\\)\\[\\([0-9]*\\)x[0-9]*\\].*/\\1(" & myText & ")\\/Run \\2\\//'") as text
do shell script "mkdir -p " & newPath & " ; mv " & quoted form of aFile & space & newPath
end repeat
This gives the following error: error "Can’t get text item 2 of \"testFolder\"." number -1728 from text item 2 of "testFolder"
Let me clarify: I have a bunch of files in a folder named /testFolder/ (always named that, all files enter here) what I want is to move the files into folders and subfolders in a given format based on the file names.
Example: File: /UZLA 55879 [01x05] XXX.cdf
Base name "UZLA 55879", the folder /UZLA 55879 (LCMS)/ exists at the destination. the (LCMS) is irrelevant to the move, it's just extra junk on the folder name, the script should detect that the folder exists (despite what junk comes between the ()" and use it as it's destination. If no folder with that base name exist it can just pop up an error or crash the whole script, that's not really the issue as new base names are rarely created and are named manually (and rather randomly) anyway.
The second part of the name is [01x05] the first part of that, "01" is detected (stripped from its padding zero) and moved into subfolder /Run 1/ (If it's "[05x07] is goes into /Run 7/ etc. the rest of the file name/extention is irrelevant to the move.
Current issue: The script now tries to pull the info from the starting folder to choose the destination folder, the starting folder is (not) named /UZLA 55879 (LCMS)/ it uses that "LCMS" to create the destination folder. (which it can't since the starting folder is 1) not named that and 2)) every destination folder has a different item (some are the same though) between those parentheses so naming the starting folder like that is useless. The script uses " & myText & " for that, that string has to be a random string which is defined by the destination folder not the starting folder.
I would normally not answer a question like this unless the user has put some effort into it, but I am a sucker for regular expressions.
set sourceFolder to "/Users/You/Desktop/testFolder"
set destFolder to "/Users/You/Desktop/testFolder2"
set myFiles to every paragraph of (do shell script "find " & quoted form of sourceFolder & " \\! -name \".*\" -type f")
repeat with aFile in myFiles
set aFile to aFile as text
tell application "Finder" to set fileName to name of (POSIX file aFile as alias)
-- Find name of parent folder
set parentName to do shell script "echo " & quoted form of fileName & " | sed 's/ \\[.*//'"
tell application "System Events" to set parentFolder to POSIX path of (first folder of folder destFolder whose name starts with parentName)
-- Extract the text between ( and )
set myText to do shell script "echo " & quoted form of parentFolder & " | sed 's/.*(\\(.*\\)).*/\\1/'"
set newPath to quoted form of (destFolder & "/" & (do shell script "echo " & quoted form of fileName & " | sed -e 's/\\[0/\\[/' -e 's/\\([^\\[]*\\)\\[\\([0-9]*\\)x[0-9]*\\].*/\\1(" & myText & ")\\/Run \\2\\//'") as text)
do shell script "mkdir -p " & newPath & " ; mv " & quoted form of aFile & space & newPath
end repeat

Resources