15.9 C
London
Monday, September 16, 2024

Unable to rename file in applescript


I’ve code working that can course of a number of information dropped on my app. The processing takes an mp4 file and utilizing VLC replaces the audio codec with one other codec creating an identically named file however with a m4v suffix. The unique mp4 file is then deleted. These steps work high quality.

I then want to rename the newly created m4v file suffix to mp4. This fails miserably. I’ve googled, I’ve learn articles. I am clearly not understanding the niceties of POSIX, alias, and file.

I am offering a pared-down model of the app that will get paths and creates filenames. It does the mp4 delete and fails on the m4v rename.

The present code iteration fails with this error:

Can’t set identify of file “Information Quantity:The Gilded Age S02 E01.m4v” to “The Gilded Age S02 E01.mp4”.
Finder received an error: Can’t set identify of file “Information Quantity:The Gilded Age S02 E01.m4v” to “The Gilded Age S02 E01.mp4”. (-10006)

Any insights to level me in the suitable course can be appreciated.

    --##################################################################
    --# Pattern snippet assuming: 
    --#  - file "Information Quantity:The Gilded Age S02 E01.mp4" exists and is dropped on this app
    --#  - the "mp4" file is processed creating "Information Quantity:The Gilded Age S02 E01.m4v"
    --#  - the "mp4" file is then deleted
    --# - the "m4v" file is renamed to the "mp4" file.   THIS FAILS
    --##################################################################
    
    --############################################
    --# this code processes every file dropped on this app
    --############################################
    on open theDroppedItems
        repeat with a from 1 to size of theDroppedItems
            set aDroppedItem to merchandise a of theDroppedItems
            show dialog "fundamental(): aDroppedItem = " & aDroppedItem
            
            set aDroppedItem_FullPath to the POSIX path of aDroppedItem
            show dialog "fundamental(): aDroppedItem_FullPath = " & aDroppedItem_FullPath
            
            set aDroppedItem_FullPath_NoSfx to remove_file_suffix(aDroppedItem_FullPath)
            show dialog "fundamental(): aDroppedItem_FullPath_NoSfx = " & aDroppedItem_FullPath_NoSfx
            
            set aDroppedItem_Filename to call of (data for aDroppedItem_FullPath) as string
            show dialog "fundamental(): aDroppedItem_Filename = " & aDroppedItem_Filename
            
            set aNewFile_FullPath to aDroppedItem_FullPath_NoSfx & ".m4v" as string
            show dialog "fundamental(): aNewFile_FullPath = " & aNewFile_FullPath
            
            set aNewFile to aNewFile_FullPath as POSIX file
            show dialog "fundamental(): aNewFile = " & aNewFile
            
            -- delete the unique "mp4" file
            show dialog "fundamental(): delete aDroppedItem = " & aDroppedItem
            inform software "Finder"
                strive
                    transfer aDroppedItem to trash
                finish strive
            finish inform
            
            -- rename the brand new "m4v" file to "mp4"
            show dialog "fundamental(): rename aNewFile = " & aNewFile
            show dialog "fundamental(): rename aDropped_Filename = " & aDroppedItem_Filename
            inform software "Finder"
                -- this fails
                set identify of aNewFile to aDroppedItem_Filename
            finish inform
            
        finish repeat
    finish open
    
    --############################################
    --# this sub-routine removes the file suffix
    --############################################
    on remove_file_suffix(theText)
        if size of theText = 0 then
            error "Cannot trim empty textual content." quantity -1728
        else if size of theText = 1 then
            return ""
        else
            return textual content 1 through -5 of theText
        finish if
    finish remove_file_suffix

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here