You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

"Can't get POSIX file" error from AppleScripts

I'm trying to write an InDesign script with AppleScript, I'm very new to AppleScripting(and Bash) and I'm struggling to find a way to accomplish a certain task. The goal of the InDesign script is to get all of the files in a group of directories and then export them as pdf. I'm able to get all of the files in a single directory like this

tell application "Finder"
	set MyList to files of folder POSIX file "/Users/User/Desktop/Group-of-folders/folder-with-several-files" as alias list
end tell
repeat with aFile in MyList
	tell application "Adobe InDesign CC 2019"
		set fileName to do shell script "basename " & quoted form of (POSIX path of aFile) & "| cut -d. -f1"
		set myDocument to open aFile
		tell active document
			export format PDF type to "/Users/User/Desktop/All PDF Exports" & fileName & ".pdf"
		end tell
		close active document saving no
	end tell
end repeat


This works fine for just one folder but I would like the script to make a list of all the sub-directories inside the "Group-of-folders" and then repeat the above script inside each directory.


I've tried various combinations of the following and I keep getting the error "Can't get POSIX file \"/Users/user....?\"


tell application "Finder"
	set myFolderList to folders of folder POSIX file "/Users/User/Desktop/Group-of-folders/" as alias list
	repeat with aFolder in myFolderList
		set filePath to (POSIX path of aFolder)
		set MyList to files of folder POSIX file filePath as alias list
	end repeat
end tell


error "Finder got an error: Can’t get POSIX file \"/Users/User/Desktop/Group-of-folders/Sub-folder-1/\"." number -1728 from file "Apple SSD:Users:User:Desktop:Group-of-folders:Sub-folder-1:"

Posted on May 21, 2019 4:39 PM

Reply
Question marked as Top-ranking reply

Posted on May 21, 2019 5:16 PM

Use the following:


tell application "Finder"

set myFolderList to folders of folder "Desktop:Group-of-folders" of home

repeat with aFolder in myFolderList

set MyList to files of aFolder as alias list

end repeat

end tell


(171775)

Similar questions

3 replies

May 22, 2019 6:56 AM in response to masonkieth

Incase anyone stumbles upon this type of problem in the future, here's the template I'm saving for myself. I thought this would be an easy template to find and it wasn't. Not for me anyways.


This script only searches two levels of directories, which is all I needed right now. (group of folders<folder 1, folder 2, folder 3< product docs inside of: folder 1, folder 2, folder 3...) I'm sure you can achieve multiple levels of directories by using some try/repeat statements. I will work on that later, though :)


tell application "Finder"
	--Set up folder list us myFolderList to hold array of subdirectories with the specified folder
	set myFolderList to folders of folder "Desktop:X-Stream Datasheets" of home
	(*repeat cycles through the list of folders in myFolderList and assisgns the file path of
	each subdirectory in the folder to aFolder. Every pass of the repeat function changes to the next folder in the list*)
	repeat with aFolder in myFolderList
		set MyList to files of aFolder as alias list -- assign each file (via the file's path) to MyList	
		repeat with aFile in MyList
			(*Like the repeat task above, each file is cycled through until no more files 
		are available and the top repeat task is restarted.*)
			tell application "Adobe InDesign CC 2019"
				--Set preferences is optional. This preference turns off alerts
				set MyPreference to script preferences
				tell MyPreference
					set user interaction level to never interact
				end tell
				(*set the file name to the basename of the file path and the cut the fiile extension of it. 
				With out the do shell script your file will save as /file/path/of/doc.indd.pdf*)
				set fileName to do shell script "basename " & quoted form of (POSIX path of aFile) & "| cut -d. -f1"
				set myDocument to open aFile
				tell active document
					export format PDF type to "/Users/masonkieth/Desktop/All PDF Exports - DataSheets/" & fileName & ".pdf" --add path where indesign will export and reassign a file extension
				end tell
				close active document saving no --optional
			end tell
			--end all the remaining tasks
		end repeat
	end repeat
end tell

"Can't get POSIX file" error from AppleScripts

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.