26.6 C
New York
Sunday, March 30, 2025

Macos – Applescript – Delete chosen information and folders whose file identify accommodates a sure chain


First, apologize for my n00bness. I’ve an issue with a code that I managed to stitch collectively. We work with template information (normally PSD), all saved collectively in folders, which have a generic identify. We copy them and get rid of these that aren’t crucial for the challenge, and the change of mass identify The generic chain, which in our case is an X. Inside these folders there are additionally folders and waste information which have some particular identify (that is related later).

The templates will comprise within the identify of item_a or item_b relying on which challenge they’re designed, drafts or finals relying on the State.

Due to this fact, a typical file identify can be item_a_x_draft, the place x can be changed by the small print of the present challenge.

The script model that I’ll use for this matter will take the present choice in Finder, will get rid of any file whose names comprise the Factor or the ultimate chains, along with different chains that belong to the information or unfastened folders, and can request a textual content entry that can be used to interchange the X between the primary two subsidies within the file identify.

    inform software "Finder"
    set selectedItems to choice
    set replacementText to textual content returned of (show dialog "Occasion#_AssetName" default reply "")
    
    -- Course of every merchandise
    repeat with i from 1 to size of selectedItems
        set currentItem to merchandise i of selectedItems
        set currentName to call of currentItem
        if ¬
            identify of currentItem accommodates ("ITEM_A") ¬
            or identify of currentItem accommodates ("FINAL") ¬
            or identify of currentItem accommodates ("belongings") ¬
            then
            delete currentItem
        else
            if currentName accommodates "ITEM_B" then
                -- Exchange the primary "X" between underscores within the filename with the required textual content
                set modifiedName to my replaceFirstX(currentName, replacementText)
                set identify of currentItem to modifiedName
            finish if
        finish if
    finish repeat
finish inform


-- Operate to interchange the primary 'X' between underscores within the filename
on replaceFirstX(fileName, newText)
    -- Verify if the file has an extension by discovering the final interval (.)
    set fileExtensionPos to offset of "." in fileName
    if fileExtensionPos is larger than 0 then
        -- Cut up the file identify and extension
        set fileExtension to textual content (fileExtensionPos + 1) via finish of fileName
        set fileNameWithoutExtension to textual content 1 via (fileExtensionPos - 1) of fileName
    else
        -- If there isn't any extension, deal with it as a traditional file identify
        set fileNameWithoutExtension to fileName
        set fileExtension to ""
    finish if
    
    -- Discover the primary underscore
    set firstUnderscorePos to offset of "_" in fileNameWithoutExtension
    
    -- Discover the final underscore by looking from the top of the string
    set lastUnderscorePos to offset of "_" in (reverse of characters of fileNameWithoutExtension as string)
    
    -- Convert the place from reverse again to common place
    set lastUnderscorePos to (size of fileNameWithoutExtension) - lastUnderscorePos + 1
    
    -- Verify if each underscores exist and are legitimate
    if firstUnderscorePos shouldn't be 0 and lastUnderscorePos shouldn't be 0 then
        -- Discover the half earlier than the primary underscore and after the final underscore
        set beforeUnderscore to textual content 1 via firstUnderscorePos of fileNameWithoutExtension
        set afterUnderscore to textual content (firstUnderscorePos + 1) via (lastUnderscorePos - 1) of fileNameWithoutExtension
        
        -- Exchange the X between the underscores
        set newMiddle to my replaceText(afterUnderscore, "X", newText)
        
        -- Kind the modified file identify, preserve the extension intact
        set afterLastUnderscore to textual content (lastUnderscorePos + 1) via finish of fileNameWithoutExtension
        
        return beforeUnderscore & newMiddle & "_" & afterLastUnderscore & "." & fileExtension
    else
        -- No X discovered, return authentic identify with extension
        return fileNameWithoutExtension & "." & fileExtension
    finish if
finish replaceFirstX

-- Operate to interchange textual content inside a string
on replaceText(theText, searchText, replaceText)
    set AppleScript's textual content merchandise delimiters to searchText
    set theTextItems to textual content gadgets of theText
    set AppleScript's textual content merchandise delimiters to replaceText
    set theText to theTextItems as string
    set AppleScript's textual content merchandise delimiters to {""}
    return theText
finish replaceText

The script works properly, with none error, offered that any subfolder within the choice shouldn’t be prolonged, based on This screenshot Leading to thisthe place something was eradicated whose identify contained “closing” or “item_a”, and “take a look at” changed the unique “X” within the file identify of the left ingredient.

Nonetheless, if I’ve expanded subfects (and their chosen content material, in fact) as in This screenshotThe script will return This error

I perceive why this occurs, and that the code might be rubbish. In my view, I would really like the script to work “from high to backside” and if the subfolders coincide with the standards that can be eradicated, they should be eradicated along with their content material and clearly the content material ignored within the script utterly, even when in idea they’re a part of the choice that’s being processed, which is what I believe is launching the error (one thing just like the script that tries to course of one thing that has been processed and discover it))

I’m a complete rookie by the best way, so I hope I made some sense, and if I didn’t, I’m very joyful to (strive) make clear. Additionally apologies, having the ability to insert photos would have been extra environment friendly, however I’ve no consultant!

Are there strategies right here to unravel issues like this?

Related Articles

Latest Articles