applescript strange behaviour with rename file script - image

I wrote this simple script that changes filenames of the folder Images on my desktop. The script works on the first run, however when I rerun it with a different string for the variable newName (same string would throw error: filename already exists). It only changes the name of the even numbers of "item x of the Folder".
Can someone enlighten me on how this happens and what to do to avoid this peculiar behaviour. I'm having a hard time figuring this out.
Thank you very much in advance.
tell application "Finder"
set theFolder to ((path to desktop folder) & "Images") as string as alias
set myImages to the name of every file of theFolder
set indexOfFolder to (count items in myImages) as number
set newName to "whateverName" as string
repeat with x from 1 to indexOfFolder
set name of item x of theFolder to newName & x & ".JPG"
end repeat
end tell
edit: After some more testing, it appears that the problem would not point exclusively to unchanged uneven numbers. With more then 20 files it appears to switch from 20 on to the even numbers that will not be renamed.

It seems to me that you are working on the name of the Folder, and also the chars of the folder (items of the folder)
Please compare with this that should work correctly:
tell application "Finder"
set theFolder to ((path to desktop folder as text) & "Images")
set myImages to the name of every file of folder theFolder
set indexOfFolder to (count myImages)
set newname to "Z picture " as string
repeat with x from 1 to indexOfFolder
set name of item x of folder theFolder to newname & x & ".JPG"
end repeat
end tell
It still isn't bug free, next discrepancy is that you use files, when gathering the contents into the myImages variable, but you address the item of the folder, which are two different object classes. I have also, finally set the folder as the in this last implementation, to avoid redundancy. Pleas also observe where I have placed the coercions, and which I have removed, because they were redundant.
tell application "Finder"
set theFolder to folder ((path to desktop folder as text) & "Images")
set myImages to the name of every file of theFolder
set indexOfFolder to (count myImages)
set newname to "Z picture " as string
repeat with x from 1 to indexOfFolder
set name of file x of theFolder to newname & x & ".JPG"
end repeat
end tell
This version, should work even though when files changes names, as the file-list is obtained up front.
tell application "Finder"
set theFolder to folder ((path to desktop folder as text) & "Images")
set myImages to every file of theFolder
set indexOfFolder to (count myImages)
set newname to "Z picture " as string
repeat with x from 1 to indexOfFolder
set name of item x of myImages to newname & x & ".JPG"
end repeat
end tell

Related

AppleScript Loop Print All File Folder Names Path

Help I need to print into the output of IDE all of the folder and file names contained in a folder. Here is my code so far:
set Music_Collection_Folder to POSIX file "/Volumes/A_Live HD/Music/Collection" as alias
set the_string to ""
tell application "Finder"
repeat with this_item in (get items of ("/Volumes/A_Live HD/Music/Collection"))
set the_string to the_string & name of this_item & tab & modification date of this_item & tab & size of this_item & tab & kind of this_item & return
end repeat
end tell
You created an alias of the target folder. Use it!
The Finder has no idea what a POSIX path is
repeat with this_item in (get items of Music_Collection_Folder) ...

Apple Script runs, but no saved docx appear (no errors appear)

This code runs, but for some reason no files are saved after running it and no errors appear. Ideas?
AppleScript
tell application "Finder"
set fl to files of folder POSIX file "/Users/abc/folder" as alias list
end tell
repeat with f in fl
tell application "Microsoft Word"
activate
open f
set theActiveDoc to the active document
end tell
delay 1
tell application "System Events"
keystroke "a" using command down
keystroke "c" using command down
end tell
tell application "Finder"
set filename to name of f
set the_path to POSIX file "/Users/abc/folder2/" & filename
end tell
tell application "Microsoft Word"
close theActiveDoc saving no
set new_document to make new document
paste special (text object of selection) data type paste text
save as new_document file name the_path
close active document
end tell
end repeat
solution
set the_path to (POSIX file "/Users/abc/folder2/" as text) & filename
Here is a completely different route. This solution bypasses having to use Microsoft Word, completely. I've configured this script to loop through every file in a designated folder (assuming all of the files in this folder are either Microsoft word documents, plain text files, or rich text files)… takes the content of each file, and creates a new .docx file with that content in a new folder
If this code is saved as an application, you can drag files and or folders, directly onto this application's Icon, to be processed. If this application is directly launched, it will ask you whether you want to process a file or files, or process a folder with all of its contents
on open theFiles2
-- Handle the case where the script is launched by dropping
-- file or folders onto this applet's icon
tell application "Finder"
set theFiles to files of entire contents of folder theFiles2 as alias list
end tell
repeat with aFile from 1 to count of theFiles
set thisFile to item aFile of theFiles
set thisFile2 to thisFile
set thisFile to POSIX path of thisFile
tell application "Finder"
set fileName to name of thisFile2
set fileName to (characters -5 thru 1) of fileName as string
set thePath to (path to desktop as text) & fileName
try
set newFolder to make new folder ¬
at (path to desktop) ¬
with properties {name:fileName}
end try
end tell
set thePath to POSIX path of alias thePath
set theScript to do shell script "textutil -convert docx " & quoted form of ¬
thisFile & " -output " & quoted form of thePath & quoted form of fileName & ".docx"
end repeat
end open
on run
-- Handle the case where the script is launched without any dropped files
set sourceFolder to display dialog ¬
"Would You Like To Choose A Folder With All Of Its Contents Or Individual Files Within A Folder?" buttons {"Cancel", "Select Files", "Select Folder"} ¬
default button 3 ¬
cancel button ¬
"Cancel" with title ¬
"withTitleText" with icon 1 ¬
giving up after 20
if button returned of sourceFolder is "Select Folder" then
set sourceFolder to ((choose folder) as text)
tell application "Finder"
set theFiles to files of entire contents of folder sourceFolder as alias list
end tell
repeat with aFile from 1 to count of theFiles
set thisFile to item aFile of theFiles
set thisFile2 to thisFile
set thisFile to POSIX path of thisFile
tell application "Finder"
set fileName to name of thisFile2
try
set fileName to (characters -5 thru 1) of fileName as string
end try
set thePath to (path to desktop as text) & fileName
try
set newFolder to make new folder ¬
at (path to desktop) ¬
with properties {name:fileName}
end try
end tell
set thePath to POSIX path of alias thePath
set theScript to do shell script "textutil -convert docx " & quoted form of ¬
thisFile & " -output " & quoted form of thePath & quoted form of fileName & ".docx"
end repeat
else if button returned of sourceFolder is "Select Files" then
set theFiles to choose file with prompt ¬
"Choose Files" invisibles false ¬
multiple selections allowed true ¬
as text
repeat with aFile from 1 to count of theFiles
set thisFile to item aFile of theFiles
set thisFile2 to thisFile
set thisFile to POSIX path of thisFile
tell application "Finder"
set fileName to name of thisFile2
try
set fileName to (characters -5 thru 1) of fileName as string
end try
set thePath to (path to desktop as text) & fileName
try
set newFolder to make new folder ¬
at (path to desktop) ¬
with properties {name:fileName}
end try
end tell
set thePath to POSIX path of alias thePath
set theScript to do shell script "textutil -convert docx " & quoted form of ¬
thisFile & " -output " & quoted form of thePath & quoted form of fileName & ".docx"
end repeat
end if
end run
This code may be a little sloppy and there may b a better solution but... It seems to work pretty well on my system running the latest version of macOS High Sierra

Drill Deeper In Recursive Processing

I have a folder that is three levels deep, each level contains a good amount of files. I am able to get two levels deep with the script below, however the third level is posing a bit of a challenge. Would someone mind offering some guidance as to how I am supposed to get one level deeper? Use python or another language wouldn't be acceptable, as I am trying to see how this works with AppleScript.
set sourceFolder to (choose folder)
tell application "Finder"
my changeFileNameCase(sourceFolder, "upper")
repeat with subFolder in (get every folder of folder sourceFolder)
my changeFileNameCase(subFolder as alias, "upper")
#This Is No Good
repeat with theFolder in (get every folder of folder subFolder)
my changeFileNameCase(theFolder, "upper")
end repeat
end repeat
end tell
on changeFileNameCase(targetFolder, caseToSwitchTo)
tell application "Finder"
set fileList to every file of folder targetFolder
repeat with theFile in fileList
set oldName to name of theFile
set newName to my changeCaseOfText(oldName, caseToSwitchTo)
set the name of theFile to newName
end repeat
end tell
end changeFileNameCase
To make the handler recursive you have to get all items of the folder and check the class of each item.
If the class is folder call the handler passing the folder
If the class is file rename it
set sourceFolder to (choose folder)
changeFileNameCase(sourceFolder, "upper")
on changeFileNameCase(targetFolder, caseToSwitchTo)
tell application "Finder"
set theList to every item of targetFolder
repeat with i from 1 to count theList
set theItem to item i of theList
if class of theItem is folder then
my changeFileNameCase(theItem, caseToSwitchTo)
else
set oldName to name of theItem
set newName to my changeCaseOfText(oldName, caseToSwitchTo)
set name of theItem to newName
end if
end repeat
end tell
end changeFileNameCase

Append filenames with folder name [dont understand]

Im struggling to understand how to rename(append) filenames inside a folder with the folders name.
Eg, about.txt, picture.jpg, etc to become folderName-about.txt, folderName-picture, etc.
Im a complete noob at applescript, this is my first script ever!
on run {input, parameters}
tell application "Finder"
set theFolder to folder "OS X:Users:David:Desktop:SCRIPT:script-copy"
set targetFolder to folder "OS X:Users:David:Desktop:SCRIPT:script-copy-finished"
display dialog "Which structure do you wish to duplicate?" buttons {"Structure-MAIN", "Structure-OTHER"}
set chosenStructure to button returned of result
if contents of chosenStructure is equal to "Structure-MAIN" then
set chosenStructure to "OS X:Users:David:Desktop:SCRIPT:script-copy:-MAIN"
else
set chosenStructure to "OS X:Users:David:Desktop:SCRIPT:script-copy:-OTHER"
end if
display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)
set createNewStructure to make new folder at targetFolder with properties {name:newName}
duplicate every file of entire contents of folder chosenStructure to createNewStructure
set the_folder to name of folder chosenStructure
repeat with this_file in (get files of entire contents of folder chosenStructure)
set the_start to offset of "_" in ((name of this_file) as string)
set the_stop to count (name of this_file as string)
set name of this_file to (the_folder & (items the_start thru the_stop of (name of this_file as string)))
end repeat
end tell
return input
end run
Thanks for your help!
I want all filenames inside the folder to have the folders name
prepended at the beginning of the filename. Eg, about.txt to become
folderName-about.txt, etc.
Try this:
tell application "Finder"
...
set the_folder to name of createNewStructure
repeat with this_file in (get files of entire contents of createNewStructure)
set name of this_file to the_folder & "-" & (name of this_file)
end repeat
end tell
The whole Script:
set theFolder to ((path to desktop) as text) & "SCRIPT:script-copy"
set targetFolder to ((path to desktop) as text) & "SCRIPT:script-copy-finished"
display dialog "Which structure do you wish to duplicate?" buttons {"-MAIN", "-OTHER"}
set chosenStructure to ((path to desktop) as text) & "SCRIPT:script-copy:" & button returned of result
display dialog "Specify a new folder name:" default answer "John The Dog"
set newName to (text returned of result)
tell application "Finder"
set createNewStructure to make new folder at targetFolder with properties {name:newName}
duplicate every file of entire contents of folder chosenStructure to createNewStructure
set the_folder to name of createNewStructure
repeat with this_file in (get files of entire contents of createNewStructure)
set name of this_file to the_folder & "-" & (name of this_file)
end repeat
end tell

AppleScript Overwrite or Skip Command?

I have the following applescript that takes files and puts them in their corresponding folders.
set sourceFolder to choose folder
tell application "Finder"
set theFiles to files of sourceFolder
repeat with aFile in theFiles
set fileName to name of aFile
if fileName contains "#" then
set poundOffset to offset of "#" in fileName
set folderName to text 1 thru (poundOffset - 2) of fileName
set newFolder to (sourceFolder as text) & folderName & ":"
if not (exists folder newFolder) then
make new folder at sourceFolder with properties {name:folderName}
end if
move aFile to folder newFolder
end if
end repeat
end tell
It works great for me except in the case of a file conflict. If there's a file with the same name in the folder that it's putting it in the script gets an error and crashes. So my question is this....How do I fix that? I'm willing for it to just overwrite the file but is there a way to bring up a prompt to skip the file or just skip it all together and move on to the next one?
I'm a little fuzzy on what I can do here. Thanks in advance for the help.
Ringslinger.
Try:
move aFile to folder newFolder replacing true

Resources