2

I feel my AppleScript skills are sorely lacking and hope to learn from this effort.

I have code working that will process one or more files dropped on my app. The processing takes an ".mp4" file, and using VLC, replaces the audio codec with another codec creating an identically named file but with an ".m4v" suffix. The original ".mp4" file is then deleted. These steps work fine.

I then wish to rename the newly created ".m4v" file suffix to ".mp4". This fails miserably. I've Googled and read articles. I'm obviously not understanding the niceties of POSIX, alias, and file.

I'm providing a pared-down version of the app that gets paths and creates filenames. It does delete the ".mp4" file but fails to rename the ".m4v" file.

The current code iteration fails with this error:

Can’t set name of file "Data Volume:The Gilded Age S02 E01.m4v" to "The Gilded Age S02 E01.mp4". Finder got an error: Can’t set name of file "Data Volume:The Gilded Age S02 E01.m4v" to "The Gilded Age S02 E01.mp4". (-10006)

Any insights to point me in the right direction would be appreciated.

--##################################################################
--# Sample snippet assuming: 
--#  - file "Data Volume:The Gilded Age S02 E01.mp4" exists and is dropped on this app
--#  - the "mp4" file is processed creating "Data Volume: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 each file dropped on this app
--############################################
on open theDroppedItems
    repeat with a from 1 to length of theDroppedItems
        set aDroppedItem to item a of theDroppedItems
        display dialog "main(): aDroppedItem = " & aDroppedItem
        
        set aDroppedItem_FullPath to the POSIX path of aDroppedItem
        display dialog "main(): aDroppedItem_FullPath = " & aDroppedItem_FullPath
        
        set aDroppedItem_FullPath_NoSfx to remove_file_suffix(aDroppedItem_FullPath)
        display dialog "main(): aDroppedItem_FullPath_NoSfx = " & aDroppedItem_FullPath_NoSfx
        
        set aDroppedItem_Filename to name of (info for aDroppedItem_FullPath) as string
        display dialog "main(): aDroppedItem_Filename = " & aDroppedItem_Filename
        
        set aNewFile_FullPath to aDroppedItem_FullPath_NoSfx & ".m4v" as string
        display dialog "main(): aNewFile_FullPath = " & aNewFile_FullPath
        
        set aNewFile to aNewFile_FullPath as POSIX file
        display dialog "main(): aNewFile = " & aNewFile
        
        -- delete the original "mp4" file
        display dialog "main(): delete aDroppedItem = " & aDroppedItem
        tell application "Finder"
            try
                move aDroppedItem to trash
            end try
        end tell
        
        -- rename the new "m4v" file to "mp4"
        display dialog "main(): rename aNewFile = " & aNewFile
        display dialog "main(): rename aDropped_Filename = " & aDroppedItem_Filename
        tell application "Finder"
            -- this fails
            set name of aNewFile to aDroppedItem_Filename
        end tell
        
    end repeat
end open

--############################################
--# this sub-routine removes the file suffix
--############################################
on remove_file_suffix(theText)
    if length of theText = 0 then
        error "Can't trim empty text." number -1728
    else if length of theText = 1 then
        return ""
    else
        return text 1 thru -5 of theText
    end if
end remove_file_suffix

1 Answer 1

2

It looks like Finder isn't getting along with the POSIX file coercion, but it doesn't really get along with POSIX files in general. An easy fix is to also coerce the POSIX file to alias, for example:

set aNewFile to aNewFile_FullPath as POSIX file as alias

When working with a mix of POSIX and HFS/APFS paths, using System Events is usually easier, since it can handle text paths for both. Some commands and properties may be slightly different, for example the name extension in System Events is read-only, but overall it is less quirky:

on run -- example
   open (choose file with multiple selections allowed)
end run

on open droppedItems
   repeat with anItem in droppedItems
      set posixPath to the POSIX path of anItem
      set pathWithoutExt to removeFileExt(posixPath)
      tell application "System Events" to set filename to name of disk item posixPath
      set newFilePath to pathWithoutExt & ".m4v"
      display dialog "original item: " & (anItem as text) & return & return & "
posixPath = " & posixPath & "
pathWithoutExt = " & pathWithoutExt & "
filename = " & filename & "
newFilePath = " & newFilePath with title "Testing "
      display dialog "Delete " & quoted form of posixPath & return & return & "Rename " & quoted form of newFilePath & " to " & quoted form of filename with title "Delete and Rename"
      tell application "System Events"
         move disk item posixPath to trash
         set name of disk item newFilePath to filename
      end tell
   end repeat
end open

on removeFileExt(filePath) -- remove any file extension from the path
   set filePath to filePath as text
   tell application "System Events" to set extension to name extension of disk item filePath
   if extension is "" then return filePath
   return text 1 thru -((count extension) + 2) of filePath
end removeFileExt
1
  • red_menace, thank you for the solution. Adding the "as alias" fixed the issue. And thank you for the suggested coding changes. I'll review and incorporate. Nov 2, 2023 at 23:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .