Rename duplicated file with applescript based on excel/txt list - excel

I am having some issues with making a duplicating and renaming a list of files based on an excel file.
In the excel file in column 1 there is the original name. In column 2 I put in the new name.
There is a base folder with a lot of images, only the images in the excel file should be duplicated and renamed.
It is important that the duplicating and the renaming is done at once, because it is possible that the same base file is needed more than once in the destination file.
Tried a lot with renaming, but I only get the copying working.
set listFile to POSIX path of (choose file)
set dataList to paragraphs of (read listFile as «class utf8»)
tell application "Finder"
set ImageFile to "Macintosh HD:Users:joostjenneskens:Pictures:Base"
set theFolder to "Macintosh HD:Users:joostjenneskens:Pictures:Destination"
repeat with thisFileName in dataList
with timeout of 1200 seconds
duplicate (files of folder ImageFile whose name starts with (thisFileName as text)) to theFolder with replacing and exact copy
end timeout
end repeat
end tell
Thanks in advance for your help.

rename the duplicate values in excel or txt file format.

In your script, there is nothing about the destination file names. I assume every line in tour table contains a pair of tab-separated filenames
You should extract every items's columns with something like this :
set AppleScript's text item delimiters to {tab} -- or ";" for CSV
copy (every text item of thisFileName) to {sourceFileName, destinationFileName}
set AppleScript's text item delimiters to {} -- dirty reset (you should save/restore the previous settings)
then you can use the two variables sourceFileName and destinationFileName
and compute the full paths sourceFullPathName and destFullPathName
I would try this for the copy action
log "Copying" & space & sourceFileName & " to " & destinationFileName
do shell script "cp" & space & quoted form of posix path of sourceFullPathName & space & quoted form of posix path of destFullPathName
type "man cp" in your terminal to select suitable options (with "cp -v")
BTW it could be possible to create links or aliases with "ln" instead of "cp"
the terms
(files of folder ImageFile whose name starts with (thisFileName as text))
should return a list of source files…
With the "cp" mode you should add a nested loop to copy them, but how to figure out what's the destination name ?
hope this could help…

Related

insert lines of text into an already existing .txt/.xml file?

I've been assigned to update a large, existing XML file with some data from an excel file. I want to use a VBA code for a quicker way to update since the number of excel rows and text lines is quite large and can change in the future.
I've already searched and found varying amount of code on how to read and write into a txt/xml file (doesn't matter which). However I haven't been able to find any on how to write in an already existing txt/xml file without deleting all of it's already existing content.
Just for example purposes, I've written bellow a text on which to work on (I'm not using the original text since it's hundreds of lines long).
How to improve on life!
Pro tips:
Enjoy!
What I would want is to be able to insert multiple lines of text in that space, either by using a text line as a reference (after "Pro tip:" or before "Enjoy!") or by detecting an empty line and use that as a starting point. I do not know the number of text lines and excel rows, however I do have some reference points in that the file has a preexisting structure, thus I can use the text in some lines as points of reference.
Is it possible to do this in excel VBA?
A text file is sequential. That means that after a line follows immediately the next line. There is no room to insert.
To insert a line in an existing text file, you need to "spool" the file and at the place where you want to "insert" the line, write it and then spool the remainder. The following is an example:
Sub spool(myFile As String, myPath As String)
Dim textline
Open myPath & myFile For Input As #1
Open myPath & "tmp.txt" For Output As #2
While Not EOF(1)
Line Input #1, textline
' do your check here and insert the line
If (myCondition = True) Then
Print #2, "Hello World!"
End If
Print #2, textline
Wend
Close #1
Close #2
Kill myPath & myFile
Name myPath & "tmp.txt" As myPath & myFile
End Sub

Save workbook generated from an Excel Template in different locations

I have an Excel xltm template which is used to produce workbooks that must be saved in one of two folders depending on the value the user enters into a specific cell in the workbook. I know how to determine the content of the cell and therefore make a decision as to which folder to save the workbook in and the destination folders will always be in the same location relative to the folder where the template is stored. However, different users may have the template folder and destination folders on different drives so I cannot use a hard coded path for the destination folders in the macro. I know I could open a file dialog and get the user to select the folder but would prefer to have the macro perform the save directly. I've tried using ThisWorkbook.Path but this doesn't work as there is no path associated with the workbook when it is created from the template. Any ideas would be appreciated.
If the amount of available (or varying) drives is reasonably limited, you can hard code the expected drives into an Array and run a loop checking if each drive/file path combination is available.
When the file path is available, the book will save and the loop will end.
Maybe something like this:
Sub SaveMe()
Dim FilePath As String: FilePath = "\Users\urdearboy\Documents\"
Dim Drive, i As Long
Drive = Array("D:", "Z:", "C:")
For i = LBound(Drive) To UBound(Drive)
If PathExists(Drive(i) & FilePath) Then
ThisWorkbook.SaveAs (Drive(i) & FilePath & "Book Name Here" & ".xlsx")
Exit For
End If
Next i
End Sub
Function to test if folder path exists
Function PathExists(path As String) As Boolean
If Dir(path) <> vbNullString Then
PathExists = True
End If
End Function
If you do decide to use this, I would just keep this procedure separate from your other code and call this sub when it is time to save a new book. Of course you will need to pass your variable Folder path (and probably a book name) into the above macro. Then, this should be able to save with dynamic drive & folder locations
Update - not quite the solution unfortunately! I assumed Environ("OneDriveCommercial") would return the path of the users Sharepoint folder but it doesn't - it returns the path to the users personal OneDrive folder, the same as Environ("OneDrive"). There appears to be no environment variable for finding the location of the Sharepoint folder. Instead, I used filepath = Left(Environ("OneDrive"), Len(Environ("OneDrive")) -24) & "xxxx" This gets a string equal to the path of where the user's personal OneDrive folder is, removes the characters (24 in my case) equal to the OneDrive folder name (and thus at the start of the sharepoint folder) and then appends the string (shown as xxxx) which is the remainder of the path to the required folder within the sharepoint folder.
So, for example, for User1 in company named Company Name1:
If their Personal One Drive folder is at C:\Users\User1\OneDrive - Company Name1 and their Sharepoint folder is at C:\Users\User1\Company Name1, Environ("OneDrive") returns C:\Users\User1\OneDrive - Company Name1 and filepath = Left(Environ("OneDrive"), Len(Environ("OneDrive")) -24) & "xxxx" removes the characters OneDrive - Company Name1 and the appends the characters xxxx set to equate to the path to the required folder in the sharepoint folder. This starts with Company Name1 followed by the rest of the path.

How do I rename multiple folder/subfolder contents with automator without affecting the folders themselves?

I'm trying to rename a bunch of files with automator so they are sequential. All the files are already in the correct order.
I tried:
Get Selected Finder Items
Get Folder Contents (checked repeat for each subfolder found)
Rename Finder Items: Make Sequential
I'm giving each item a new name so it appears with 3 digits (001.xxx) and continues from there.
I'm only wanting to rename the folder contents. For example. I have a folder with 10 subfolders. In each subfolder are 20 images with names like image001, image002, etc. I want to rename the images as 001, 002, 003. But I want this to start over for each subfolder.
When I run the automator script above, It will rename the first subfolder 001, then it will name the first image (image001) inside the first subfolder 002. The last image (image020) will be 021, then the 2nd subfolder would be renamed 022, the first image of it (image001) will be renamed 023, and so on.
How can I get it to leave the names of the subfolders alone and just rename the contents (image001 = 001, etc) but once it finishes with the 20 images in the first subfolder, it goes to the 2nd subfolder and names those images 001 to 020 as well?
Thanks for your time.
This is too much for Automator; it is not able to perform repeats of this nature. However, it is a simple job for AppleScript:
set f to choose folder -- Select Master Folder
tell application "Finder"
set subF to every folder of folder f
repeat with eachFolder in subF
set counter to 1
set allFiles to (every file of eachFolder)
repeat with oneFile in allFiles
set newFileName to my pad(counter, 3)
set correctExtension to oneFile's name extension
set oneFile's name to (newFileName & "." & correctExtension)
set counter to counter + 1
end repeat
end repeat
end tell
on pad(num, howLong)
set thisNum to num as text
set c to count thisNum
repeat howLong - c times
set thisNum to "0" & thisNum
end repeat
return thisNum as text
end pad
This should work; it is a modification of a script that I have used in the past. However, before running it on your real files, make a copy somewhere first to try it out, and reply here if there are any problems.

How do I search for a folder and hyperlink its path?

I am working on a spreadsheet in which I will keep track of everything that my department 3D prints. I have 3 folders full of one thing each for every part I print: a CAD file, an STL file, and a job folder; all of these contain the same part name which is entered into a cell in the spreadsheet and used for finding all of the files/folders. I like to have my spreadsheet hyperlinked so that I can easily open up any of the three by simply navigating to that part in my spreadsheet.
Now I have the following section of code which takes the name of the printed part from column D, and finds the matching STL in my STL folder, and then hyperlinks it into column U.
For i = 4 To Range("D" & Rows.Count).End(xlUp).Row
'follow through all entries in column D
'--STL------------------------------------------------------------------------------------------------------
If (Len(Cells(i, 4)) > 1) Then
If (Len(Cells(i, 21)) = 0) Then
strFile = Dir$(path5w & Cells(i, 4) & "*.stl")
If (Len(strFile) > 1) Then
Cells(i, 21).Hyperlinks.Add Cells(i, 21), path5 & strFile, TextToDisplay:="STL"
Else
'No file was found that matches so do nothing
End If
Else
'Already hyperlinked, skip this cell
End If
Else
'Not a valid Name, do nothing
End If
Next
End Sub
I simply copied this chunk of code again and switched the path and switched .STL the extension for my CAD files, and it works great for both of those, but I am getting stuck on the job folders... I have no idea how to get my code to find a folder instead of a file.
I have tried playing around with FileSystemObjects, but I don't fully understand how to use them, and all I can find is an example of how to list every folder inside a folder, and not how to actually search for a specific folder.
I also looked at this example: VBA to find multiple files but, again, I run into the problem of not understanding how to use this to search for a folder, rather than listing all folders.
So to help be more clear I will give an example. Lets say I process Part123.stl, when I want to save this, it will create a folder ssys_Part123 and I will save that in my folder named Job Folders. Now I want my program to check cell D4 which says Part123, then navigate to Job Folders, find the folder named ssys_Part123 and hyperlink that folder into V4.
I still don't have a very firm grasp of coding, so any help is always greatly appreciated.
If the folder name is always going to be ssys_[PartName] then you should be able to just concact the strings to link to the folder instead of trying to look up the folder name.

Trying to Use AppleScript to Convert .xls and .xlsx to .txt (Tab Delimited), Needs Fixing

I copied this from an answer I got from another thread. I am trying to convert ~300 .xls and .xlsx files to tab delimited. They are all in the same folder. If anyone knows a better way, please let me know.
property type_list : {"XLS6", "XLS7", "XLS8", "XLSX"}
property extension_list : {"xls", "xlsx"}
on open these_workbooks
repeat with k from 1 to the count of these_workbooks
set this_item to item k of these_workbooks
set the item_info to info for this_item
--this if statement tests to make sure the items you're converting are Excel spreadsheets and not folders or aliases
if (folder of the item_info is false) and (alias of the item_info is false) and ((the file type of the item_info is in the type_list) or the name extension of the item_info is in the extension_list) then
tell application "Finder" to open this_item
tell application "Microsoft Excel"
--this just tacks on ".txt" to your file name
set workbookName to (name of active workbook & ".txt")
--save the current open workbook as a tab-delimited text file
tell active workbook to save workbook as filename workbookName file format text Mac file format
close active workbook saving no
end tell
end if
end repeat
end open
on run
display dialog "Drop Excel files onto this icon."
end run
All this does is open a dialog box and does nothing. Even though it's meant to be a droplet, nothing happens when I drag a file to it.
In Applescript the run handler gets run when the script or application is run normally. Meanwhile, the open VarName handler gets run when some file or files are dropped onto the icon of the application and the files get set to the variable VarName. The script you posted cleverly places that display dialog in the on run handler to try to help you understand this usage. Instead, save the script as an application and then drop your files onto it.
EDIT:
After a quick test on a Mountain Lion machine that finally has Excel 2011 (I didn't realize how much I'd been overcomplicating):
property type_list : {"XLS6", "XLS7", "XLS8", "XLSX"}
property extension_list : {"xls", "xlsx"}
on open these_workbooks
repeat with k from 1 to the count of these_workbooks
set this_item to item k of these_workbooks
set the item_info to info for this_item
--this if statement tests to make sure the items you're converting are Excel spreadsheets and not folders or aliases
if (folder of the item_info is false) and (alias of the item_info is false) and ((the file type of the item_info is in the type_list) or the name extension of the item_info is in the extension_list) then
tell application "Finder" to open this_item
tell application "Microsoft Excel"
--this just tacks on ".txt" to your file name
set workbookName to (path to desktop as string) & "After:" & (name of active workbook & ".txt")
display dialog workbookName
--save the current open workbook as a tab-delimited text file
tell active workbook to save workbook as filename workbookName file format text Mac file format
close active workbook saving no
end tell
end if
end repeat
end open
on run
display dialog "Drop Excel files onto this icon."
end run

Resources