Append filenames with folder name [dont understand] - applescript

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

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

applescript strange behaviour with rename file script

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

AppleScript to create aliases of files in a folder

So I have this AppleScript for making aliases of all the contents of one folder to another folder.
I works perfectly unless there is only one item in the source folder.
tell application "Finder"
set SourceFolder to (choose folder with prompt "Please choose Source folder:") as string
set DestinationFolder to (choose folder with prompt "Choose Destination folder for aliases:") as string
set theFolders to every item of entire contents of folder SourceFolder
repeat with thisFolder in theFolders
set thisName to name of thisFolder
make new alias file at folder DestinationFolder to thisFolder without invisibles
end repeat
end tell
Any idea why it's not getting anything when there's one item only? When there's at least 2 items in the source folder it creates aliases for both in the destination.
On a side note, any way to get it to remember the source and destination folders between times you run it?
Try:
property SourceFolder : missing value
property DestinationFolder : missing value
if SourceFolder = missing value then
set SourceFolder to (choose folder with prompt "Please choose Source folder:")
set DestinationFolder to (choose folder with prompt "Choose Destination folder for aliases:")
else
tell application "Finder"
set theFolders to every item of entire contents of SourceFolder as list
repeat with thisFolder in theFolders
make new alias file at DestinationFolder to thisFolder
end repeat
end tell
end if

Searching/duplicating multiple files in folder using Applescript

I have a text file of around 500 selected file names (each on its own line) from an event I photographed with over 3,000 pictures. I want to be able to find all those images in the folder, duplicate them, and move those duplicated files into a different folder.
This is what I have so far. Right now it just copies the entire folder of 3,000 images and puts it into the destination folder, but not the individual files..
TEXT FILE:
_EPW0847.jpg
_EPW0848.jpg
_EPW0853.jpg
etc....
APPLESCRIPT:
set thePhotos to paragraphs of (read (choose file with prompt "Choose a text file"))
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theName in thePhotos
try
tell application "Finder" to duplicate alias (theSourceFolder & theName) to theDestination with replacing
end try
end repeat
tell application "Finder"
tell folder theDestination to set theCount1 to (count of items) as string
end tell
set theCount2 to (count of thePhotos) as string
display dialog (theCount1 & " of " & theCount2 & " items copied to " & theDestination) buttons {"OK"}
Any help would be great. I don't know apple script that well yet, but I'm learning. Thanks!
You were close!
set thePhotos to paragraphs of (read (choose file with prompt "Choose a text file"))
set theSourceFolder to (choose folder with prompt "Choose source folder")
set theDestination to (choose folder with prompt "Choose destination folder")
set dupeList to {}
repeat with theName in thePhotos
try
set end of dupeList to alias ((theSourceFolder as text) & theName)
end try
end repeat
tell application "Finder" to duplicate dupeList to theDestination with replacing
set theCount1 to (count of dupeList) as text
set theCount2 to (count of thePhotos) as text
display dialog (theCount1 & " of " & theCount2 & " items copied to " & (theDestination as text)) buttons {"OK"}
If there are blank lines in the text file, try this:
set thePhotos to paragraphs of (read (choose file with prompt "Choose a text file"))
set theSourceFolder to (choose folder with prompt "Choose source folder")
set theDestination to (choose folder with prompt "Choose destination folder")
set dupeList to {}
repeat with theName in thePhotos
try
if theName ≠ "" then
set end of dupeList to alias ((theSourceFolder as text) & theName)
end if
end try
end repeat
set fileContents to read (choose file with prompt "Choose a comma-delimited text file")
set theText to result
set AppleScript's text item delimiters to ","
set theTextItems to text items of theText
set AppleScript's text item delimiters to {""}
theTextItems
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theEPSName in theTextItems
tell application "Finder"
set theEPSFile to theSourceFolder & theEPSName
move file theEPSFile to folder theDestination with replacing
end tell
end repeat

Resources