Using applescript to move files whose names appear in an excel spreadsheet - excel

I have a folder with about 4000 image files. I need to select some of those files, based on whether their names appear in an excel spreadsheet, and copy them to a new folder. If possible, I would also like to tag them with a color tag so I know which files in the original folder were moved and which were not. I got the code below from here.
tell application "Microsoft Excel"
tell worksheet 1 of active workbook
set fileList to value of used range
end tell
end tell
set rootFolder to "path/to/sourcefolder" as POSIX file
set filesToMove to {}
repeat with thisItem in fileList
try
set end of filesToMove to alias (rootFolder & thisItem)
on error
display dialog "File " & thisItem & " is not in the folder"
end try
end repeat
tell application "Finder"
move filesToMove to folder "path/to/destination" as POSIX file
end tell
When I run it, it's unable to find any of the files. I think the issue might have to do with using / in my file path instead of : but I don't know how to set a path to an external drive with the applescript syntax. Using the same variable declaration, set rootFolder to "path/to/sourcefolder" as POSIX file, and then telling finder to open rootFolder works fine.
I also need the script to have more flexibility - not all files have the same naming conventions, and so I need it to search for any files whose names contain the identifiers from the spreadsheet, but may not match it exactly. I.E. the spreadsheet entry may be "00103", but the file name is "PX00103KL.jpg."
In the meantime, I've created an automator workflow that moves and tags the files exactly the way I want, but only after I manually enter the search string. If I could combine those two ideas, of iterating through the spreadsheet entries in excel and using each in turn as input for the automator workflow, that would be ideal.
Thanks for any help!

I don't have Excel, but assuming that you can get a list of strings from it, here is a bit of code that will find all files in a target folder that contain the string(s), set a label to it and then copy it. Change appropriately.
It uses the unix command line tool for spotlight, mdlist to find the files. Trying to do it purely in applescript seems to be painful. The shell command when stitched together is something like this:
mdfind -onlyin ~/Documents/ "kMDItemDisplayName == '*somestring*'"
Here is the code:
set stringList to {"144", "g_"}
set rootFolder to "/Users/blah/Downloads/" as POSIX file
set destFolder to "/Users/blah/Downloads/test" as POSIX file
set filesToMove to {}
set mdAtr to "\"kMDItemDisplayName == '*"
repeat with thisItem in stringList
set sPath to quoted form of POSIX path of rootFolder
set sName to mdAtr & thisItem & "*'\""
set sCmd to "mdfind -onlyin " & sPath & " " & sName
set sResults to (do shell script sCmd)
set fList to (every paragraph of sResults) as list
repeat with i in fList
set f to (POSIX file i) as alias
tell application "Finder"
set label index of f to 3
#copy file f to folder (destFolder as alias)
end tell
end repeat
end repeat

Related

AppleScript - How to append text to end of a file

I'd like to have a script that I can run where it basically takes the .rdp file that I have selected in finder, and append a line of text to the end of it.
e.g
I download a .rdp file to use in Microsoft Remote Access and to speed up my workflow I'd like to append the text 'Use Multimon:i:1' at the end before I launch it so that I don't have to open the preferences each time.
I'm not too familiar with AppleScript so would appreciate any advice on how to achieve this.
Thanks!
It is my understanding that .RDP files are saved in plain text format. If this is the case, using the do shell script command in an AppleScript, appending text to a file is fairly simple. This following AppleScript code should work for you.
Paste this following code into a new Script Editor.app document. Then
with your .RDP file currently selected in Finder, run the code in Script Editor.app and it will append the text to your file.
property addText : "Use Multimon:i:1"
tell application "Finder" to set selectedFile to POSIX path of ((get selection) as alias)
do shell script "echo " & quoted form of addText & " >> " & quoted form of selectedFile
This currently works for a selected file with the extension 'txt'. Test it and if it works for you, edit it to your desired extension.
The 'if' statements are to ensure that you don't accidentally append the text to a binary file which could corrupt that file. The 'return' means that your text will appear on its own line. If you don't want that, remove 'return & '. For details on 'open for access', please see the Language Guide: Commands Reference. On the same page, you can find 'close access'.
tell application "Finder"
set tFile to selection as alias
if name extension of tFile is "txt" then
set corR to true
else
display alert "Are you sure you've selected the correct file?"
set corR to false
end if
end tell
if corR is true then
set ab to open for access tFile with write permission
write return & "Use Multimon:i:1" to ab starting at eof
close access ab
end if

Opening folder from a cell that contains the path and the filename too

I would like to open the folder in windows explorer from a cell that contains the filename with the path. e.g. I have "C:\files\test.xls" in the ActiveCell, and I want to open the "C:\files" folder in windows explorer, that contains the "test.xls" file.
Shell "explorer.exe " & Left$(ActiveCell.Text, InStrRev(ActiveCell.Text, "\"))
the right part retrieves the folder from the full name. The left part launches the windows explorer in that folder.
Since the path may contain spaces, this version is safer (enclosing the path with additional double-quotes):
Shell "explorer.exe """ & Left$(ActiveCell.Text, InStrRev(ActiveCell.Text, "\")) & """"

Use Automator to read a text file and copy contents to another Folder

I have a text file that contains the file names of about 500 pictures.
I would like to use Automator to:
Read the text file
Get a list of the names
Look in a 'repository folder' with about 1000 pictures
Copy the corresponding picture to a new folder.
Is that possible with Automator on Mac?
Here is what I have attempted, however it doesn't work.
Here are the Automator Actions, in order:
This code inside of the Run AppleScript Action should do the trick:
on run {input, parameters}
set imgDestination to input's item 2
set imgSource to input's item 1
set imgNameFile to choose file with prompt {"Select file of image filenames:"}
set imageList to every paragraph of (read imgNameFile)
repeat with eachName in imageList
tell application "Finder"
set targetImageFile to item 1 of (get every file in folder imgSource whose name = (eachName as text))
duplicate targetImageFile to folder imgDestination
end tell
end repeat
return input
end run
I could not devise how to get Automator to read the text file, and remember it while choosing the folders as well, so I added the choosing of the file with the image filenames as part of the AppleScript.
Good luck,

Run script on specific file type

I'm fairly new in the work of applescript and was wondering if someone can help me with this.
I'm currently writing script that will do a lot of things, one of them will be looking in a folder for files that have the .mhl extension to run a terminal command on them. I've paste the part of my script bellow. Now, this seems to work fine except fore one little detail. the .mhl files need to be at the first level of my folder. The problem is that my folder contains subfolder which contains the .mhl. Is there a way I can tell this script to look in the entire folder (subfolder include)?
Thanks a lot
set input to "/Source"
tell application "System Events" to set theFiles to POSIX path of (files of folder input whose name contains ".mhl")
repeat with i in theFiles
set filecount to 1
tell application "Terminal"
activate
do script "mhl verify -f " & POSIX path of i & ""
end tell
end repeat
Probably easier to use the find command but here is an AS approach...
set input to POSIX file "/Source"
tell application "Finder" to set theFiles to (files of entire contents of folder input whose name extension = "mhl") as alias list
tell application "Terminal"
if not (exists window 1) then reopen
activate
end tell
repeat with aFile in theFiles
set filePath to aFile's POSIX path
tell application "Terminal"
do script "mhl verify -f " & quoted form of filePath in window 1
end tell
end repeat

Finding hidden or system files containing ~ using Dir() in VBA

I'm trying to rename a series of files for SharePoint that contain illegal characters such as #, &, ~, etc. I modified the code from this site to find the files/folders to rename. When I run the following command
strTemp = Dir(strFolder & "*~*", vbHidden)
(or with any other attributes for that matter) I get a huge list of files in the directory that do not contain a ~, and yet it still does not seem to find some files that contain an initial ~ (such as temporary files caused by editing a document that were never deleted).
Anybody out there know how to locate those files?
Apologies for posting this as an answer but I don't seem able to add a comment.
ecksc has answered his own question, but I just want to point out that you don't need to replace the constants with the number 6. To combine attributes you can add them using +, for example:
strTemp = Dir(strFolder & "*~*", vbHidden + vbSystem)

Resources