Create Folders Based on Filenames at a Given Path

Hey there! Thanks for your help! I'm trying to use an AppleScript in KM, but I can't get it to work, I need to set ChosenFolder to /Users/x/Downloads/_Zoli QC Completed/Here, what am I doing wrong? It was originally set chosenFolder to (choose Folder) and that would launch a pop up window to select a directory but I don't want this step as I'll open always the same folder

set chosenFolder to (POSIX path /Users/x/Downloads/_Zoli QC Completed/Here)
tell application "Finder" to set fileList to files of chosenFolder

set TID to text item delimiters
set text item delimiters to space
repeat with aFile in fileList
    set fileName to name of aFile
    set textItems to text items of fileName
    if (count textItems) = 1 then
        set fileExtension to name extension of aFile
        set folderName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
    else 
        set folderName to first item of textItems
    end if
    set sourceFile to quoted form of POSIX path of (aFile as text)
    set destinationFile to quoted form of (POSIX path of chosenFolder & folderName & "/" & fileName)
    do shell script "ditto " & sourceFile & space & destinationFile
    do shell script "rm " & sourceFile
end repeat
set text item delimiters to TID

Share

Follow

What is the problem that you are trying to solve ?

At the moment this has the format of an XY problem, which among other things, doesn't generate a useful thread title for others searching later.


One obvious issue with your snippet is that in:

(POSIX path /Users/x/Downloads/_Zoli QC Completed/Here)

The argument of POSIX path should be a string, and you haven't wrapped it in double quotes, so it's just a sequence of 3 space-delimited tokens of uncertain meaning.

Thanks for your help. So I just want chosenFolder to be my folder located at /Users/x/Downloads/_Zoli QC Completed/Here

Why ?

What's the context ?

What are you trying to do ?

I found this script that will create folders based on filenames, but the first line opens up a pop up window to ask me to choose the folder where it will find the files. Because I'm doing a big macro, I'm trying to get rid of this pop up window and to set the ChosenFolder directly to /Users/x/Downloads/_Zoli QC Completed/Here)

Original Script:

set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder

set TID to text item delimiters
set text item delimiters to space
repeat with aFile in fileList
    set fileName to name of aFile
    set textItems to text items of fileName
    if (count textItems) = 1 then
        set fileExtension to name extension of aFile
        set folderName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
    else 
        set folderName to first item of textItems
    end if
    set sourceFile to quoted form of POSIX path of (aFile as text)
    set destinationFile to quoted form of (POSIX path of chosenFolder & folderName & "/" & fileName)
    do shell script "ditto " & sourceFile & space & destinationFile
    do shell script "rm " & sourceFile
end repeat
set text item delimiters to TID

You'll find this easier to do -- and, importantly, to maintain -- using KM actions rather than AppleScript. For starters, look at the "Split Path" action and how you can use it to get the name, minus extension, of a file so you can use it to create a folder.