How can I implement a find shell script in the applescript below?
I would like to replace
set matchfiles to (every file in (folderpath as alias) whose name starts with fnamepartthree & "_Landscape")
if matchfiles is {} then
by something like this:
do shell script "find /Volumes/TS-Leenbakker/JPEG/10/101/ -name '*_Landscape*' -type f
It would be nice if the ‘folderpath’ could be used:
set folderpath to "TS-Leenbakker:JPEG:" & fnamepartone & ":" & fnameparttwo & ":"
The reason is that the number of files is huge and using the ‘find’ in a shell is much quicker
set chosenfolder to choose folder with prompt "Kies een map met Leenbakker JPEG's" without invisibles
tell application "Finder"
set newfiles to files in chosenfolder
end tell
repeat with aFile in newfiles
set filename to name of aFile
set file_info to info for (aFile as alias)
set file_creation_date to creation date of file_info
set aFile to aFile as Unicode text
set logpath to "TS-Leenbakker:JPEG:"
my Write_Log(logpath, filename, file_creation_date)
if (length of filename) = 23 and filename contains "_Portrait" then
set fnamepartone to ((characters 1 through 2 of filename) as Unicode text)
set fnameparttwo to ((characters 1 through 3 of filename) as Unicode text)
set fnamepartthree to ((characters 1 through 8 of filename) as Unicode text)
set fnamepartfour to ((characters 1 through 23 of filename) as Unicode text)
set folderpath to "TS-Leenbakker:JPEG:" & fnamepartone & ":" & fnameparttwo & ":"
try
set folderalias to folderpath as alias
on error
do shell script "mkdir -p " & quoted form of ("/Volumes/TS-Leenbakker/JPEG/" & fnamepartone & "/" & fnameparttwo & "/")
end try
tell application "Finder"
set matchfiles to (every file in (folderpath as alias) whose name starts with fnamepartthree & "_Landscape")
if matchfiles is {} then
move (every file in (folderpath as alias) whose name is fnamepartfour) to "TS-Leenbakker:JPEG_OLD:" with replacing
move (aFile as alias) to (folderpath as alias) --with replacing
else
try
repeat with matchfile in matchfiles
move matchfile to "TS-Leenbakker:JPEG_OLD:" with replacing
end repeat
move (aFile as alias) to (folderpath as alias)
end try
end if
end tell
end if
end repeat
on Write_Log(logpath, filename, file_creation_date)
set The_Log to logpath & "log.txt"
try
open for access file the The_Log with write permission
write (return & "date processed:" & tab & ((current date) as string)) & tab & "filename: " & filename & tab & tab & "date created:" & tab & file_creation_date & return to file the The_Log starting at eof
close access file the The_Log
on error
try
close access file the The_Log
end try
end try
end Write_Log
tell application (path to frontmost application as Unicode text)
with timeout of 3600 seconds
display dialog "Beelden zijn verwerkt" buttons {"OK"} default button 1
end timeout
end tell
You can then reference the first file, for example, in the results as an applescript colon-delimited string path:
POSIX file (item 1 of matchFiles) as text
set matchfiles to (every file in (folderpath as alias) whose name starts with fnamepartthree & "_Landscape")
in the “find” command
and it’s still not very claer to me how to get the result that apllescript understands - the whole thing is new to me.
I would be a great help if one of you guys could give me an example using my script as a starter.
here is a simple example to find files,
coerce every found file to an alias and do something
set foundFiles to paragraphs of (do shell script "/usr/bin/find /Volumes/TS-Leenbakker/JPEG/10/101/ -name '*_Landscape*' -type f")
repeat with oneFile in foundFiles
set oneFileAlias to POSIX file oneFile as alias
-- do something
end repeat
you can also proceed the files directly in the shell, for example to copy all file to a specified folder
do shell script "/usr/bin/find /Volumes/TS-Leenbakker/JPEG/10/101/ -name '*_Landscape*' -type f -exec cp {} '/Path/to/destination/folder' \\;"
But I need to build in the “folderpath” with refers to the folder/subfolder where the file is and where the new file must go to.
for example: 12345678_Landscape_L.jpg (12345678_Landscape_M.jpg and 12345678_Landscape_S.jpg also always exist)
set folderpath to “TS-Leenbakker:JPEG:” & fnamepartone & “:” & fnameparttwo & “:” = TS-Leenbakker:JPEG:12:123: 12345678_Landscape.jpg
I also need “fnamepartthree” because this refers to the first eight characters of the file witch is unique
So: a “find” with “folderpath” and “fnamepartthree” would do the trick
when I’ve tried to do a find using the shell command, it errors out because I don’t have the correct permissions on a couple of the folders (which I cannot change). But those folders permit me from getting any results. Is there a way to get it to ignore permissions, or skip errors?
Programs that run as root always bypass regular permissions. If you really want to ignore permissions, you can run the find command as the root user. The way to do that is to use do shell script . with administrator privileges. I do not recommend this though, unless you really need to see what is in those read-restricted folders.
As for ignoring the errors, that is easy enough. Just add “;true” to the end of your shell command.
Usually, if find encounters an error in its traversal it will return a non-zero exit code when it eventually finishes. It does this even for non-fatal errors (like subdirectories that do not allow read or chdir), even if other useful (if partial) information is generated (i.e. the info from readable files and directories). do shell script interprets the non-zero exit code as an indication that the shell command failed. So it throws away any ˜stdout’ output that it collected (find’s normal output) and generates an AppleScript error that contains the ˜stderr’ output from the shell command (find’s permission error message will be here).
This error-on-non-zero-exit-code approach works well enough in many situations, but the way find uses the exit code can cause problems when used with do shell script. The true command (usually a shell built-in command, though there is also a binary in /usr/bin) always returns a zero exit code. true’s final, zero exit code will prevent do shell script from generating an error. do shell script’s return value will then be the normal ˜stdout’ output from the find command.