0
votes

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!

1
Is there a question here?Señor O
I dont know what to say? does the title not suggest it?user3791569
What specifically isn't working about the code you wrote?Señor O
The re-naming filenames. The last part of the script. 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.user3791569
What error or result are you getting from your file that is not what you're wanting?jweaks

1 Answers

0
votes

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