Hi guys, I am having a strange problem occurring. This AppleScript makes a folder based off of a file, and puts that file inside of the newly made folder. Sometimes it will add a period to the end of the folder (file) name, and sometimes it does not. I will never be using any periods in my filenames, if there’s not an error in my code, is there a way not to add any periods to the file? Thanks!
tell application “Finder”
activate
set selectedFiles to selection as alias list
set containingFolder to container of (item 1 of selectedFiles) --as alias repeat with i from 1 to count of selectedFiles
set foldersRef to (a reference to folders of containingFolder) set foldersRefItems to name of (contents of foldersRef) set thisitem to item i of selectedFiles
set fileName to (text items 1 thru -5) of (name of thisitem as text) as string
if fileName is not in foldersRefItems then move thisitem to (make new folder at containingFolder -
with properties {name:fileName})
text 1 thru -2 of fileName
else
move thisItem to folder fileName of containingFolder
end repeat
end tell
Can you edit your post and put three backticks (the key above the tab key) on the line above and the line below it? Ideally, the code should compile in your script editor and then you can copy/paste it in between the backticks lines.
tell application "Finder"
activate
set selectedFiles to selection as alias list
set containingFolder to container of (item 1 of selectedFiles) --as alias
repeat with i from 1 to count of selectedFiles
set foldersRef to (a reference to folders of containingFolder)
set foldersRefItems to name of (contents of foldersRef)
set thisItem to item i of selectedFiles
set fileName to (text items 1 thru -5) of (name of thisItem as text) as string
if fileName is not in foldersRefItems then
move thisItem to (make new folder at containingFolder ¬
with properties {name:fileName})
set newFileName to text 1 through ((length of fileName) - 2) of fileName
else
move thisItem to folder fileName of containingFolder
end if
end repeat
end tell
Sorry about that Ken, I’m new at this coding stuff. I have realized that filetypes with 4 letters will product the period, where filetypes with 3 will not. That’s definitely the issue, however I’m not sure how to rectify it. Thanks!
Not a problem and it’s easily remedied. It’s a pain to work with though as various characters are mangled during posting (eg quotes and dashes). By the way, you should be able to edit the existing post.
As to your issue, I would suggest using text item delimiters to split each file name on the periods. This should do what you ask. If a file name has two or more periods, then it will shed the last one and everything thereafter. If it has zero periods, then it will generate an error as the newly created folder would have to use the exact same name. If this is an issue, then would need to figure out how to deal with it… for example, you could append arbitrary characters (eg ’ ƒ’) to the folder name).
tell application "Finder"
-- activate
set selectedFiles to selection as alias list
set containingFolder to container of (item 1 of selectedFiles) --as alias
set existingFolders to (a reference to folders of containingFolder)
set existingNames to name of existingFolders -- list of existing folder names
set AppleScript's text item delimiters to "."
repeat with eachFile in selectedFiles
set fileName to name of eachFile
set nameItems to text items of fileName -- split name on '.'
set cni to count of nameItems
if cni is greater than 1 then
-- if name has but one '.' then lose the extension
set fileName to text items 1 thru -2 of nameItems as text
else -- file lacks extension
-- assume that fileName is as previously set
end if
-- NB will error if file lacks an extension
if fileName is not in existingNames then -- if no matching folder exists
move eachFile to (make new folder at containingFolder ¬
with properties {name:fileName})
else -- if matching folder already exists
move eachFile to folder fileName of containingFolder
end if
end repeat
set AppleScript's text item delimiters to "" -- reset text delimiters
end tell
This is fantastic, just perfect. Thank you so much I really appreciate the help. I was trying to do something similar on Shortcuts, but that’s even more confusing to my brain!
Since there is the possibility for periods in the name, another option would be to use whatever the Finder or System Events has determined to be the extension and split the name using that, for example:
set {fileName, _} to getNamePieces from (choose file)
to getNameAndExtension from someItem -- return name and extension from a file item
tell application "System Events" to tell disk item (someItem as text)
set {theName, theExtension} to {name, name extension}
end tell
if theExtension is not "" then
set theName to text 1 thru -((count theExtension) + 2) of theName -- the name part
set theExtension to "." & theExtension
end if
return {theName, theExtension}
end getNameAndExtension