Hey Omar,
* Note that get lastModifiedFile above does nothing useful, but if you use return lastModifiedFile it will stop execution of the script and let you see the value of lastModifiedFile. As soon as you see it's just the file name and not a path string you get a clue to why open is failing in TextEdit.
'ls' does not return a full path, therefore we have to compose the full path from the name and the original directory path.
* I don't think you mentioned that you wanted to open the file in TextEdit in your original query. Keep in mind that a lot of back-and-forth can be avoided if the question is as complete as possible.
-------------------------------------------------------------------------------------------
# Note that I've changed variable names to in an attempt to clarify:
-------------------------------------------------------------------------------------------
set _path to POSIX path of ((path to documents folder as text) & "_Output:")
set lastModifiedFileName to first paragraph of (do shell script "ls -Ft " & quoted form of _path & " | sed -E '/\\/$/d'")
# Creating the full Posix Path to the newly found file:
set lastModifiedFilePath to _path & lastModifiedFileName
# Converting the Posix Path of the file to an Alias:
set lastModifiedFileAlias to alias POSIX file lastModifiedFilePath
-------------------------------------------------------------------------------------------
tell application "TextEdit"
activate
open lastModifiedFileAlias
end tell
-------------------------------------------------------------------------------------------
trying to open file with Finder:
set _path to "/Users/okn/Documents/_Output"
set lastModifiedFile to first paragraph of (do shell script "ls -Ft " & quoted form of _path & " | sed -E '/\\/$/d'")
get lastModifiedFile
tell application "Finder"
open lastModifiedFile
end tell
--> Finder error: Handler can't handle objects of this class.
You're trying to feed the Finder a FILE NAME which is not a proper reference to the file itself.
but it works here:
tell application "Finder"
set _fldr to folder "MacHD:Users:okn:Documents:_Output"
set lastFile to (last item of (sort files of _fldr by modification date)) of alias
end tell
# Thomas Fischer, 07 Jul 2014 18:11, mE
tell application "Finder"
open lastFile
end tell
--> works fine, opens the lastFile
Why would AS treat the open cmd so different, ok it’s not a file, just a variable of a path?!
Above - of alias should be as alias.
With of alias you're getting a Finder-Reference, and then you're feeding it to the Finder. The Finder understands Finder-References. It does NOT understand file-names or Posix Paths.
Why above would you use two Finder-Tell-Blocks when one would do?
AppleScript does not treat the open command differently. You haven't been feeding it correctly. :)
--
Best Regards,
Chris