Selecting multiple files in Finder - applescript

In my applescript, I have a function which takes a selected file and writes its path into an xml file. If a folder is selected, i'm able to create a list with the files contained and send that to my function. However, if I select multiple finder items (via shift clicking), whether files or folders, I'm not able to send anything to the function.
here is the part where i get the files from the Finder
tell application "Finder"
set myPath to the selection
end tell
--if multiple files selected
if (count of myPath) is greater than 1 then
set fileList to every item of myPath
repeat with i in fileList
if (isDirectory(i)) then
else
myBigLoop(initialSuccess, i, watchFolder)
end if
end repeat
else if (isDirectory(myPath)) then
submitFolder(myPath, watchFolder)
else
set isFolder to false
set initialSuccess to true
myBigLoop(initialSuccess, myPath, watchFolder)
end if
on myBigLoop(initialSuccess, fileList, watchFolder)
repeat with myPath in fileList
if initialSuccess then
tell application "Finder"
set myFilename to myPath as alias
set myPath to the folder of myFilename
set myPath to myPath as string
set myFilename to name of myFilename
display dialog myFilename
end tell
end if --end InitialSuccess
end repeat
end myBigLoop
on isDirectory(someItem) -- someItem is a file reference
set filePosixPath to quoted form of (POSIX path of (someItem as alias))
set fileType to (do shell script "file -b " & filePosixPath)
if fileType ends with "directory" then return true
return false
end isDirectory
on submitFolder(myPath, watchFolder)
set isFolder to true
set initialSuccess to true
set fileList to item 1 of myPath
set fileList to get every file of fileList
set numFiles to count fileList
if numFiles is equal to 0 then
display dialog "There were no files in that folder."
return false
end if
myBigLoop(initialSuccess, fileList, watchFolder)
end submitFolder

I believe you should extend the Finder tell structure, because you're still working with files and folders later on:
tell application "Finder"
set myPath to the selection
--if multiple files selected
if (count of myPath) is greater than 1 then
set fileList to every item of myPath
repeat with i in fileList
if (isDirectory(i)) then
else
myBigLoop(initialSuccess, i, watchFolder)
end if
end repeat
else if (isDirectory(myPath)) then
submitFolder(myPath, watchFolder)
else
set isFolder to false
set initialSuccess to true
myBigLoop(initialSuccess, myPath, watchFolder)
end if
end tell

Related

Applescript - Recursively step through directories

I have found similar solutions like this:
property theOpenFile : missing value
tell application "Finder" to set theSel to selection
if theSel is not {} then
set pathToYourTextFile to (path to desktop folder as string) & "SampleTextFile2.txt"
set theOpenFile to open for access file (pathToYourTextFile as string) with write permission
repeat with aItem in theSel
tell application "Finder" to class of aItem is folder
if the result then my getFilesIn(aItem) -- aItem is a folder
end repeat
close access theOpenFile
end if
on getFilesIn(thisFolder)
tell application "Finder" to set theseFiles to files of thisFolder as alias list
repeat with thisFile in theseFiles
set f to thisFile as string
set pathLength to length of f
if pathLength > 255 then my writeToFile(f)
end repeat
tell application "Finder" to set theseSubFolders to folders of thisFolder
repeat with tSubF in theseSubFolders
my getFilesIn(tSubF) -- call this handler (recursively through this folder)
end repeat
end getFilesIn
on writeToFile(t)
write (t & return) to theOpenFile starting at eof
end writeToFile
They all work in with a file reference in alias format. As I understand I cannot change the filename of the original in this case, or delete it, etc. and thats what I want to do.
Am I right ? If yes what else can I do ?
You can rename and delete files, the Finder can handle alias specifiers
repeat with thisFile in (get theseFiles) -- you need get to capture the reference list
set f to thisFile as string
set pathLength to length of f
if pathLength > 255 then my writeToFile(f)
tell application "Finder" to set name of thisFile to "foo.txt"
-- or
tell application "Finder" to delete thisFile
end repeat
or rather than an alias you can use Finder file specifiers
repeat with thisFile in (get theseFiles)
set f to thisFile as string
set pathLength to length of f
if pathLength > 255 then my writeToFile(f)
tell application "Finder" to set name of file f to "foo.txt"
-- or
tell application "Finder" to delete file f
end repeat

applescript list of droppeditems not displaying properly

i have the following code:
on open theDroppedItems
set PosixList to {}
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
set PosixPath to convertPathToPOSIXString(theCurrentDroppedItem)
copy PosixPath to the end of the PosixList
end repeat
display dialog convertListToString(PosixList, space)
end open
on convertPathToPOSIXString(thePath)
tell application "System Events"
try
set thePath to path of disk item (thePath as string)
on error
set thePath to path of thePath
end try
end tell
return POSIX path of thePath
end convertPathToPOSIXString
on convertListToString(theList, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theString to theList as string
set AppleScript's text item delimiters to ""
return theString
end convertListToString
I expect it to allow me to drop a file onto the applescript (saved as an application) and display a list similar to /Users/adam/a.txt /users/adam/b.txt however, it gives me 2 dialog's, 1 with the path to the first file, then the second with the path to the second file. What am i missing?
thanks!
I think this shortened version is more along the lines of what you are looking for?
on open theDroppedItems
set posixList to {}
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
set posixPath to POSIX path of theCurrentDroppedItem
set end of posixList to (posixPath & linefeed & linefeed)
end repeat
display dialog posixList as string
end open

Applescript: if file contains any of names move to specific folder

I'm trying to make a script that will do this:
There will be a .txt file with a list of names, one per paragraph, like this:
name1
name2
name3
name4
name5
There also will be a folder with lots of files in it.
I want the script to look at every file in that folder, and in any of the names listed in .txt file are contained in the file name, then move that file to a specific folder. If the file name does not contain one of the names from the list, then move that file to a different folder.
Example:
- There are "name1", "name2" and "name3" in the .txt file.
- There are "000name2000.jpg", "000name7000.jpg" and "000name3000.jpg" in the folder.
- "000name2000.jpg" contains "name2" in it so it should be moved to folder1.
- "000name3000.jpg" should be moved to folder1 as well.
- And "000name7000.jpg" should be moved to folder2.
I want it to be so because this list with names will be very long and I want script to be as small as possible.
This is how my script looks like now:
property source_folder : alias "path:to:source_folder"
property tattoos_folder : alias "path:to:first_folder"
property models_folder : alias "path:to:second_folder"
property text_file : alias "path:to:text_file.txt"
process_folder(source_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
on process_item(this_item, c, i)
if i < 10 then
set i to "000" & i
else if (i < 100) and (i > 9) then
set i to "00" & i
else if (i < 1000) and (i > 99) then
set i to "0" & i
end if
set r to (random number from 0 to 9999)
if r < 10 then
set r to "000" & r
else if (r < 100) and (r > 9) then
set r to "00" & r
else if (r < 1000) and (r > 99) then
set r to "0" & r
end if
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 first_folder -- THIS IS WHERE THIS NEW PART OF CODE SHOULD BE
end if -- IF NAME IS (meets name from .txt file) THEN MOVE TO first_folder,
end tell -- IF ANOTHER THEN MOVE TO second_folder
end process_item
display notification "All images were processed." with title "New" sound name "Glass.aiff"
tell me to quit
This assumes you can define your text file and folder paths in the script. You can add choose dialogs if you need to.
This script puts all files in the source folder into a list, then it goes through each file, checking if a name in the name list matches, and if it does, moves it to folder1, otherwise, if it is still left after checking all the names, then moves it to folder2.
-- define folder/file locations
property nameFile : ((path to desktop) & "nameList.txt") as string
property sourceFolder : ((path to desktop) & "test:") as string
property folder1 : ((path to desktop) & "folder1:") as string
property folder2 : ((path to desktop) & "folder2:") as string
try
set move1List to {}
set move2List to {}
set nameList to paragraphs of (read file nameFile)
set fileList to list folder sourceFolder without invisibles
repeat with n from 1 to count (fileList)
set thisFile to item n of fileList
-- set fileName to name of (info for alias (sourceFolder & thisFile))
set fileRef to alias (sourceFolder & thisFile)
repeat with thisName in nameList
if (thisName as string) is in thisFile then
set end of move1List to fileRef
exit repeat
end if
end repeat
if exists fileRef then set end of move2List to fileRef
end repeat
display dialog "About to move: " & return & (count move1List) & " files to folder1" & return & (count move2List) & " files to folder2"
-- move all the files in the lists
tell application "Finder" -- can try "System Events"
move move1List to folder folder1
move move2List to folder folder2
end tell
-- could avoid Finder by iterating to a shell script using
-- do shell script "mv " & quoted form of thisFile & space & quoted form of folder1
on error err
display dialog err
end try

Folder image to iTunes artwork in applescript : not working anymore

Anyone of you guys could point me to why this applescript (which was working on snow leopard) does not work anymore on 10.7 Lion.
It intend to copy the file named "folder.jpg" from each album folder into ID3 tags of selected mp3 in iTunes.
property tempfile : ((path to temporary items as string) & "itunespicturefile_temporaire.pict")
global vPictFolder
tell application "iTunes"
set v_TrackSelection to selection
if v_TrackSelection is not {} then
repeat with vTrack in v_TrackSelection
set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
set thisPict to my getpictData("folder.jpg")
if thisPict is not "" then
delete artworks of vTrack
set data of artwork 1 of vTrack to (thisPict)
end if
end repeat
else
display dialog ("select tracks first !")
end if
end tell
on getpictData(vAlbumpictName)
tell application "Finder" to tell file vAlbumpictName of folder vPictFolder to if exists then
set t_file to it as string
else
display dialog vPictFolder & vAlbumpictName
return ""
end if
do shell script "/opt/local/bin/convert " & quoted form of POSIX path of t_file & " " & quoted form of POSIX path of tempfile
display dialog "ok"
return read (tempfile as alias) from 513 as picture
end getpictData
on ParentFromPath(thePath)
set wantPath to true
set thePath to (thePath as text)
set saveDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set pathAsList to text items of thePath
if the last character of thePath is ":" then
set idx to (the number of text items in thePath) - 2
else
set idx to -2
end if
if wantPath then
set folderName to ((text items 1 through idx of pathAsList) as text) & ":"
else
set folderName to item idx of pathAsList
end if
set AppleScript's text item delimiters to saveDelim
return folderName
end ParentFromPath
Because Picture format (".pict") is not supported on Lion and newer OS.
You can use the term picture to read a image file (JPEG, PNG, ...), the format will not be in the Picture format , but the original format of the file.
tell application "iTunes"
set v_TrackSelection to selection
if v_TrackSelection is not {} then
repeat with vTrack in v_TrackSelection
set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
set thisPict to my getpictData(vPictFolder & "folder.jpg")
if thisPict is not "" then
delete artworks of vTrack
set data of artwork 1 of vTrack to (thisPict)
end if
end repeat
else
display dialog ("select tracks first !")
end if
end tell
on getpictData(tFile)
try
return (read (tFile as alias) as picture)
end try
display dialog tFile
return "" -- not exists
end getpictData

Applescript File naming

Code snippet:
tell application "Finder" to set new_item to ¬
(container of this_item as string) & (name of this_item) & "s"
save this_image in new_item as typ
Its saving the file as filename.pngs I want it to save as filenames.png. Any ideas how to fix this?
Save this as an application, then you can drop things on it and it will copy them adding an s to the name. Or you can double-click it and it will ask you to choose a file to copy. NOTE: change the property appendText if you want to change the text (i.e. "s") that's added to the name.
Notice the handler getName_andExtension(f). That's what I use to get the name and extension from a file. I use that to calculate the new path when adding the s.
I hope that helps!
property appendText : "s"
on run
set f to choose file
duplicateInPlace_appendingText(f, appendText)
end run
on open the_items
repeat with f in the_items
duplicateInPlace_appendingText(f, appendText)
end repeat
end open
on duplicateInPlace_appendingText(f, textToAppend)
set f to f as text
set {nm, ex} to getName_andExtension(f)
tell application "Finder" to set theContainer to (container of item f) as text
if ex is "" then
set new_path to theContainer & nm & textToAppend
else
set new_path to theContainer & nm & textToAppend & "." & ex
end if
do shell script "cp -R " & quoted form of POSIX path of f & space & quoted form of POSIX path of new_path
end duplicateInPlace_appendingText
on getName_andExtension(f)
set f to f as text
set {name:nm, name extension:ex} to info for file f without size
if ex is missing value then set ex to ""
if ex is not "" then
set nm to text 1 thru ((count nm) - (count ex) - 1) of nm
end if
return {nm, ex}
end getName_andExtension
name of this_item gives the full name of the Finder item, including the file extension. You need to split it up. Unfortunately, this isn't as easy as it could be. You could do something like:
tell application "Finder"
get the selection
set {dispname, ext, exthidden} to {displayed name, name extension, extension hidden} of item 1 of the result
end tell
if exthidden then
dispname & "s." & ext
else
((characters 1 through -(2 + (count of ext)) of dispname) as string) & "s." & ext
end if
That just builds the modified name for the first item in the Finder selection, without doing anything with it.

Resources