Help with POSIX path

I am trying the following and an error is returned. " error “Can’t get POSIX path of {alias "Macintosh HD:Users:todd:Desktop:addy_ozzie:", "addy_ozzie 2022-12-26 15_18.mp4"}.” number -1728 from POSIX path of {alias “Macintosh HD:Users:todd:Desktop:addy_ozzie:”, “addy_ozzie 2022-12-26 15_18.mp4”}"

Below is the script:

> set theFolder to choose folder
> set theFiles to list folder theFolder without invisibles
> 
> repeat with aFile in theFiles
> 	if aFile does not contain "-ql" then
> 		set theFile to (POSIX path of (theFolder & aFile))
> 		set theFileName to name of theFile
> 		set theFileExtension to name extension of theFile
> 		
> 		if theFileExtension is not "mp4" then
> 			set theNewFile to (POSIX path of (path to desktop as text)) & theFileName & ".mp4"
> 			do shell script "ffmpeg -i " & quoted form of theFile & " -vcodec h264 -acodec aac -strict -2 " & quoted form of theNewFile
> 			set theFile to theNewFile
> 		else
> 			set theFile to (POSIX path of theFile)
> 		end if
> 		
> 		do shell script "qlmanage -t -s 2048 -o " & quoted form of theFile
> 		set theNewFileName to text 1 thru -5 of theFileName & "-ql" & text -4 thru -1 of theFileName
> 		set theNewFile to (POSIX path of (theFolder & theNewFileName))
> 		do shell script "mv " & quoted form of theFile & " " & quoted form of theNewFile
> 	end if
> end repeat

I am not sure why there is an error, any ideas?

You need to add “as text” to “(POSIX path of (theFolder & aFile))” like so

set theFile to (POSIX path of (theFolder as text & aFile))

This is because “theFolder” contains an alias, not text

LuckBox. Even after resolving the POSIX-path issue, I don’t believe your script will work correctly. For example, POSIX path is just a string and the script cannot get its name and name extension.

There are a lot of ways to do what you want, but I’ve included a suggestion below that uses the Finder. It first gets the file information and then calls a handler that does all the other stuff (which should be done outside the Finder tell statement).

BTW, the reason you are receiving an error is that you join (concatenate) an alias and a string, which creates a list. POSIX path is a property of alias and file objects and doesn’t know what to do with the list.

set theFolder to choose folder

tell application "Finder"
	set theFiles to every file in theFolder as alias list
	repeat with aFile in theFiles
		set fileName to name of aFile
		set fileExtension to name extension of aFile
		processTheFile(aFile, fileName, fileExtension) of me
	end repeat
end tell

on processTheFile(theFile, fileName, fileExtension)
	set posixFile to POSIX path of theFile
	set newFile to (POSIX path of (path to desktop)) & fileName
	-- do other stuff
end processTheFile

Here is a version that does everything without callouts to Finder

set theFolder to choose folder
set theFiles to list folder theFolder without invisibles
set theFolder to POSIX path of theFolder
set tid to text item delimiters
set text item delimiters to "."
repeat with aFile in theFiles
	set theFile to contents of aFile
	if theFile does not contain "-ql" then
		set theFileName to text items of theFile
		if (count theFileName) > 1 then
			set theFileExtension to text item -1 of theFile
			set theFileName to (items 1 thru -2 of theFileName) as text
		else
			set theFileName to theFileName as text
			set theFileExtension to ""
		end if
		set theFile to (theFolder & theFile)
		if theFileExtension is not "mp4" then
			set theNewFile to (POSIX path of (path to desktop as text)) & theFileName & ".mp4"
			try
				do shell script "/usr/local/bin/ffmpeg -i " & quoted form of theFile & " -n -vcodec h264 -acodec aac -strict -2 " & quoted form of theNewFile
			on error errMsg number errNUm
				display alert errMsg & return & "Error #" & errNUm
			end try
			set theFile to theNewFile
		end if
		"qlmanage -t -s 2048 -o " & (quoted form of theFolder) & " " & (quoted form of theFile)
		do shell script "qlmanage -t -s 2048 -o " & (quoted form of theFolder) & " " & (quoted form of theFile)
		set theNewFileName to theFileName & "-ql." & theFileExtension
		set theNewFile to POSIX path of (theFolder & theNewFileName)
		do shell script "mv " & quoted form of theFile & " " & quoted form of theNewFile
	end if
end repeat
set text item delimiters to tid

EDIT fixed some things

By the way, what does qlmanage do to an “mp4” file?

Having never attempted this before, I thought I’d check it out.

It opens a finder window a la quicklook but does not play the video within the window. Instead, it sits paused while displaying the opening frame of the video. As when using quicklook normally, the window does come with a play button but it must be clicked manually in order to begin playing the movie. Finally, the window title has [DEBUG] prepended to the file’s name.

Thanks for the assistance, but this just seems to hang and never finishes.

Why did you use the " - o " switch? This causes it to create an image file that I’m assuming you don’t need or want.

also

using System Events you can get just the visible files by using a “whose” clause

set theFiles to every file in theFolder whose visible is true

This works but it slows down the results

the sample below lets “System Events” sort out the invisibles, but is slower

tell application "System Events"
	set theFiles to every file in theFolder whose visible is true
end tell

the next sample lets AppleScript remove the invisibles and is faster

tell application "System Events"
	set {paths, isVis} to {path, visible} of every file in theFolder --whose visible is true
end tell
set theFiles to {}
repeat with i from 1 to count paths
	if (item i of isVis) then set end of theFiles to item i of paths
end repeat

Enjoy

Sorry,

Still not used to the new forum layout.

1 Like

It’s rare that anyone uses the list folder command–which is part of Standard Additions–and I decided to see how it performs when creating POSIX paths. I used a folder with 100 files and tested four options in Script Geek. The results were:

Script One (list folder command) - 11 milliseconds
Script Two (Finder) - 164 milliseconds.
Script Three (System Events) - 5 milliseconds
Script Four (ASObjC) - 1 millisecond

A few comments:

  • The scripts were run on a Monterey computer.
  • The list folder command is deprecated.
  • Scripts one and four return files and folders. Script two returns files and may return hidden files. Script three returns both files and hidden files.
  • The ASObjC script is to set a benchmark and is not intended for actual use by the OP.

The test scripts are:

-- SCRIPT ONE (11 milliseconds)
set theFolder to "Store:Test Folder:" as alias
set theFiles to list folder theFolder without invisibles
repeat with aFile in theFiles
	set contents of aFile to ((POSIX path of theFolder) & aFile)
end repeat

-- SCRIPT TWO (164 milliseconds)
set theFolder to "Store:Test Folder:" as alias
tell application "Finder" to set theFiles to every file in theFolder as alias list
repeat with aFile in theFiles
	set contents of aFile to POSIX path of aFile
end repeat

-- SCRIPT THREE (5 milliseconds)
set theFolder to "Store:Test Folder:" as alias
tell application "System Events" to set theFiles to POSIX path of every file in theFolder

-- SCRIPT FOUR (1 millisecond)
use framework "Foundation"
use scripting additions
set theFolder to "Store:Test Folder:" as alias
set theFolder to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFolder)
set fileManager to current application's NSFileManager's defaultManager()
set theFiles to ((fileManager's contentsOfDirectoryAtURL:theFolder includingPropertiesForKeys:{} options:4 |error|:(missing value))'s valueForKey:"path") as list

Interesting comparison. Thanks for sharing.