0
votes

This should be easy.

Our company has filenames that use this format: yyyymmdd.brand.customername.pics

Objective: for each FILE, create a new FOLDER with the same name, without the file extension.

Example of automation objective:
Source File: 20210301.ABC.Jones.pics (file)
Target Result: 20210301.ABC.Jones (folder)

I've been trying unsuccessfully to obtain the name of the source file that excludes the path.

My automation is a folder action (here are the pertinent steps)

  1. When file dropped into a specific folder, set variable TPEPathname = the filename (with path)
  2. Get the value of variable TPEPathname from Step 1, pass the value into the next step.
  3. Obtain a reference to the file using Get Selected Finder Items, pass the reference to the Finder object into the next step
  4. Run AppleScript to obtain the name of the Finder object filename EXCLUDING THE PATH (see attached photo for the AppleScript)

Step 3 is successful. Step 4 returns nothing. (ignore fileNamex)

I also tried to change the sequence by passing in the TPEPathname directly into the AppleScript, instead of passing in the Finder object. Same result - Step 3 is successful, Step 4 returns nothing.

Questions: Am I referencing the Step 3 Finder object correctly as an INPUT or PARAMETER within the AppleScript? The value of "Input" seems to be null. I don't see documentation on how to pass in or reference AppleScript parameters.

Will the function "fileName" in the AppleScript actually obtain and parse the filename without the path, or will the additional periods in the filename cause a problem? Acceptable result will be either: yyyymmdd.brand.customername OR yyyymmdd.brand.customername.pics

enter image description here

1
input is a list of alias specifiers. At least you need a loop for process each file separately. Apart from that an alias does not respond to fileName. And you are going to display fileName rather than fileNamex. So there are 3 syntax errors.vadian
There is only one file being passed into the AppleScript at a time; looping not needed. If I'm passing in a alias, what should I pass in instead, or what function should I use to extract the name from the alias? I've displayed fileName to verify it is NULL, so this is not an actual syntax error - its a revision intended for testing: both fileName and fileNameX are NULL (and I've indicated above to ignore fileNameX)torpedo51
Regardless how many files are selected Get Finder Items passes always a list of alias specifiers to the next action (or an empty list). You can get the file name from an alias only with the Finder or System Events. Already the first line display dialog input is a syntax error. The direct parameter must be text.vadian
Got it. Where's the reference docs that explain any of this?torpedo51

1 Answers

0
votes

If I've read your OP correctly, your want to use a Folder Action which when a file is dropped, a folder based on the name of the file without the extension is created in the folder assigned to the Folder Action, and then move the dropped file into the folder created of its name.

If that is all you are trying to do, then the following Folder Action using a Run Shell Script action with Pass input: [as arguments] does exactly that. There is no need to complicate things!

In other words, you drop a file name, e.g., 20210301.ABC.Jones.pics on the folder the Folder Action is attached to and a folder named 20210301.ABC.Jones is created in the folder the Folder Action is attached to, then 20210301.ABC.Jones.pics file is moved into the 20210301.ABC.Jones folder.

Example shell script code:

    # f  = fully qualified pathname
    # d  = directory pathname
    # fn = filename with extension
    # n  = filename without extension

for f in "$@"; do
    [ -f "${f}" ] || continue
    d="${f%/*}"
    fn="${f##*/}"
    n="${fn%.*}"
    cd "${d}" || exit
    [ ! -d "${n}" ] && mkdir "${n}"
    mv -n "${f}" "${n}"
done  

enter image description here


Notes:

  • The -n option used in the mv command stops from overwriting an existing file. If you have a scenario where you want existing files overwritten, then remove the -n option used in the mv command.
  • The shell script as coded can handle multiple files dropped at the same time or just one file at a time.