choose from list result to shell

hi,
I’ve started working on a script that will look for files and copy them to a destination but I’m stuck at the choose from list and parse the result to a shell copy
this is what I have soo far:

property theSDirectory : "/Volumes/blabla"

display dialog "looking for video?" default answer "" buttons {"ok", "cancel"} default button 1
if the button returned of the result is "ok" then
	set the_video to text returned of result
	
	do shell script "/usr/bin/find " & theSDirectory & " -iname " & quoted form of the_video & "*"
	set results to the result
	
	set master_list to {}
	
	repeat with i from 1 to (count of paragraphs of results)
		copy paragraph i of results to end of master_list
	end repeat
	
	set selected to choose from list master_list ¬
		with prompt "Please select:" cancel button name "Cancel" OK button name "OK" with multiple selections allowed

– here the repeat for copying the all selected from master_list

thanks in advance

  • changed the wildcard so it’s not a bash wildcard but a find wildcard.
  • not depending on the result constant, but store returned values directly into variables
  • updated the creating master list into a more efficient way.

property theSDirectory : POSIX path of (path to movies folder)

set dlr to display dialog "looking for video?" default answer "" buttons {"ok", "cancel"} default button 1
if the button returned of dlr is "ok" then
	set the_video to text returned of dlr
	
	set master_list to paragraphs of (do shell script "find " & quoted form of theSDirectory & " -iname " & quoted form of (the_video & "*"))
	
	set selected to choose from list master_list ¬
		with prompt "Please select:" cancel button name "Cancel" OK button name "OK" with multiple selections allowed
	
end if

Hi,

something like this


property theSDirectory : "/Volumes/Media/Video"

set the_video to text returned of (display dialog "looking for video?" default answer "" buttons {"ok", "cancel"} cancel button "cancel" default button 1)
set master_list to paragraphs of (do shell script "/usr/bin/find " & quoted form of theSDirectory & " -iname " & quoted form of the_video & "*")

set selected to choose from list master_list ¬
	with prompt "Please select:" with multiple selections allowed
if selected is false then return

set destination to quoted form of POSIX path of (path to desktop)
repeat with aPath in selected
	do shell script "/bin/cp " & quoted form of aPath & space & destination
end repeat

thanks guys!

next step will be some sort of output to monitor whats going on because cp will be from a NAS and lots of files may be copied plus output to the user if the name was not found in the beginning of the script
maybe create log file , open and write to it
or
the use of a progress bar

any suggestions?

added prune directory in search
can give more search with comma separator
HOWEVER:
find command finds all but list in next dialog only shows one name (last to find)


property theSDirectory : "/Users/Peter/Desktop/source"
property noSearch : "1"
property Destination : "/Users/Peter/Desktop/dest"


set theVideos to text returned of (display dialog "looking for video?" default answer "" buttons {"ok", "cancel"} cancel button "cancel" default button 1)

set oldDels to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set theVideolist to every text item of theVideos

repeat with theVideo in theVideolist
	
	set master_list to paragraphs of (do shell script "/usr/bin/find " & quoted form of theSDirectory & " -name " & noSearch & " -prune " & " -o " & " -iname " & quoted form of theVideo & "*" & " -print ")
end repeat

set selected to choose from list master_list ¬
	with prompt "Please select:" with multiple selections allowed
if selected is false then return

--set destination to quoted form of POSIX path of (path to desktop)
repeat with aPath in selected
	do shell script "/bin/cp " & quoted form of aPath & space & Destination
end repeat


set oldDels to AppleScript's text item delimiters

Well you could also create a regular expression that will do multiple searches at once:


property theSDirectory : "/Users/Peter/Desktop/source"
property noSearch : "1"
property Destination : "/Users/Peter/Desktop/dest"


set theVideos to text returned of (display dialog "looking for video?" default answer "" buttons {"ok", "cancel"} cancel button "cancel" default button 1)

set oldDels to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set theVideolist to every text item of theVideos
set AppleScript's text item delimiters to "|"
set theVideolist to theVideolist as string
set AppleScript's text item delimiters to oldDels

set regex to theSDirectory & "/.*(" & theVideolist & ").*"

set master_list to paragraphs of (do shell script "/usr/bin/find -E " & quoted form of theSDirectory & " -iregex " & quoted form of regex)

set selected to choose from list master_list ¬
	with prompt "Please select:" with multiple selections allowed
if selected is false then return

--set destination to quoted form of POSIX path of (path to desktop)
repeat with aPath in selected
	do shell script "/bin/cp " & quoted form of aPath & space & Destination
end repeat

Hmm…
That’s new to me.
What is regex / iregex and what does it do?

Thanks for the input!
Any suggestions for monitoring what’s going on? Log? Progress bar? I will need this because the whole copy action will take a while and it’s nice to see what the status is

Peter

Regex is short for regular expressions. Regular expression is a text pattern finder or comparison. You have regular expressions in almost every programming language but keep in mind that their languages are not always the same. find uses the POSIX 2 basic and extended regular expressions. It’s the same regular expression language as most command line utilities like grep, sed and awk.

About the syntax you could better read the Dutch Wikipedia page about regular expressions because the English version is maybe a bit overwhelming at first.

For the record: Command line utilities supporting POSIX 2 regular expressions still can have different outcomes because the POSIX 2 standard is not clear about every situation leaving opportunities for the developers to be creative. So don’t be surprised if there are minor differences between command line utilities were some will throw an error and others won’t.

For a progress view and personal use you could use StefanK’s SKProgressBar.

thanks, I need to look into that
Works soo far !! :slight_smile: ( only had to put in -prune again

I found the BP ProgressBar (used it in some old scripts) and will see if I can still use it
Can you help me with the following:

in order to work with the Bar I need to get the number of selected items in the list Dialog (for items remaining)
I want to trim the path of first find command to the name of the file - separator in this case is obviously “/”
this, so only the name will appear in list and bar
might not be easy because the list items contain the full path and i want to parse that on to the "do shell script cp " part
so maybe a temp list (just thinking) ?

B.t.w,

i could try the Stefan Progress Bar but I have no idea how to work with this (yet)

thanks in advance

Peter

I would get the file name on the fly. Because “/” is a forbidden character in a path it’s safe to say that when the delimiters are set to “/” that the last text item is actually the file name.

repeat with aPath in selected
	set AppleScript's text item delimiters to "/"
	set fileName to last text item of aPath
	set AppleScript's text item delimiters to ""
	--update file info field of progress bar with variable filename
	do shell script "/bin/cp " & quoted form of aPath & space & Destination
end repeat

thanks, that worked!!

But… there are stil some things to take care of.
one thing is to put the result to a log if the find command cannot find the name
so:
if not found then write to log (already have the script generate a log file on the Desktop)
the script is with an old progress bar. have looked into the one from Stefan but its not working yet…

this is what I have soo far:


property theSDirectory : "/Volumes"
property noSearch : "Macintosh HD"
--property Destination : "/Users/Peter/Desktop/dest"


set theVideos to text returned of (display dialog "looking for video?" default answer "" buttons {"ok", "cancel"} cancel button "cancel" default button 1)

set oldDels to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set theVideolist to every text item of theVideos
set AppleScript's text item delimiters to "|"
set theVideolist to theVideolist as string
set AppleScript's text item delimiters to oldDels
set theLog to (do shell script "touch " & "~/Desktop/notFound.txt " & "2>>/dev/null ")
set regex to theSDirectory & "/.*(" & theVideolist & ").*"

set master_list to paragraphs of (do shell script "/usr/bin/find -E " & quoted form of theSDirectory & " -name " & quoted form of noSearch & " -prune " & " -o " & " -iregex " & quoted form of regex & " -print ")

set selected to choose from list master_list ¬
	with prompt "Please select:" with multiple selections allowed
set listSize to (count of selected) as string
if selected is false then return

set Destination to choose folder with prompt "Choose your Destination:"

set loopCount to "0" as number

set ProgressBar to load script alias (((path to scripts folder) as text) & "Lib:BP Progress Bar Controller.scpt")
tell ProgressBar
	initialize("copy selected videos")
	barberPole(true)
	setStatusTop to "copying..."
	repeat with aPath in selected
		set AppleScript's text item delimiters to "/"
		set fileName to last text item of aPath
		set AppleScript's text item delimiters to ""
		set loopCount to ((loopCount) + 1)
		setMax to listSize
		setStatusTop to "moving:  " & fileName & " --> " & quoted form of (POSIX path of Destination)
		setStatusBottom to "Number of Items: " & loopCount & " of " & listSize
		do shell script "/bin/cp " & quoted form of aPath & space & quoted form of (POSIX path of Destination)
		increase by 1
	end repeat
	tell ProgressBar to quit
	display dialog "Videos are copied" buttons {"OK"} default button 1
	do shell script "open " & quoted form of (POSIX path of Destination)
end tell

thanks in advance