Question

I am new to applescript. I want to develop an application in applescript.

Below is my code. And while running it, it's showing finder that can't make alias. Kindly help me.

property script_name : "image Tracker"
property script_description : "image property data finder"

--set the_folder to (choose folder with prompt "Select the folder for get the image property:") as Unicode text

--set the_folder to quoted form of POSIX path of (choose folder with prompt "Select the folder for get the image property:")

set the_folder to (choose folder)

copy {"jpg", "png", "eps", "ai", "tif", "psd", "gif"} to validExtentions

tell application "Finder" to set fPaths to file the_folder
repeat with thisFilePath in fPaths
    if name extension of thisFilePath is in validExtensions then
        try
            tell application "Image Events"
                -- start the Image Events application
                launch
                -- open the image file
                set this_image to open this_file
                -- extract the properties record
                set the props_rec to the properties of this_image
                -- purge the open image data
                close this_image
                -- extract the property values from the record
                set the image_info to ""
                set the image_info to the image_info & ¬
                    "Name: " & (name of props_rec) & return
                set the image_info to the image_info & ¬
                    "*********************************************" & return
                set the image_info to the image_info & ¬
                    "File: " & (path of image file of props_rec) & return
                set the image_info to the image_info & ¬
                    "File Type: " & (file type of props_rec) & return
                set the image_info to the image_info & ¬
                    "Res: " & item 1 of (resolution of props_rec) & return
                set the image_info to the image_info & ¬
                    "Color Space: " & (color space of props_rec) & return
                copy (dimensions of props_rec) to {x, y}
                set the image_info to the image_info & ¬
                    "Dimemsions: " & x & ", " & y
            end tell
        on error error_message
            display dialog error_message
        end try
    end if
end repeat

set myfile to open for access ("Macintosh HD:Users:varghese.pt:Desktop:") & "image_data.txt" with write permission
write image_info to myfile
close access myfile

If anyone found the answer, please send me the details. Thanks in advance.

Nidhin Joseph

Was it helpful?

Solution

Let me say up front: Unfortunately, in general, AppleScript is a finicky beast, and sometimes trial and error are your only friends.

There are several problems with your code - some obvious, one not:

  • validExtentions is misspelled in the copy {"jpg ... statement - you later refer to with the (correctly spelled) name validExtensions

  • In the tell application "Finder" command, you must replace file with folder.

  • In command set this_image to open this_file, you must replace this_file with (thisFilePath as text). NOTE: Parenthesizing and as text is crucial, as the file will otherwise be opened by Finder (and therefore open in Preview.app) - don't ask me WHY this is so.

  • I also suggest replacing the hard-coded path in the write-to-file command with (path to desktop as text) & "image_data.txt"

If we put it all together, we get:

copy {"jpg", "png", "eps", "ai", "tif", "psd", "gif"} to validExtensions

set the_folder to (choose folder)

tell application "Finder" to repeat with thisFilePath in folder the_folder
    if name extension of thisFilePath is in validExtensions then
        try
            tell application "Image Events"
                # start the Image Events application
                launch
                # open the image file
                # set this_image to open thisFilePath
                set this_image to open (thisFilePath as text)
                # extract the properties record
                set the props_rec to the properties of this_image
                # purge the open image data
                close this_image
                # extract the property values from the record
                set the image_info to ""
                set the image_info to the image_info & ¬
                    "Name: " & (name of props_rec) & return
                set the image_info to the image_info & ¬
                    "*********************************************" & return
                set the image_info to the image_info & ¬
                    "File: " & (path of image file of props_rec) & return
                set the image_info to the image_info & ¬
                    "File Type: " & (file type of props_rec) & return
                set the image_info to the image_info & ¬
                    "Res: " & item 1 of (resolution of props_rec) & return
                set the image_info to the image_info & ¬
                    "Color Space: " & (color space of props_rec) & return
                copy (dimensions of props_rec) to {x, y}
                set the image_info to the image_info & ¬
                    "Dimensions: " & x & ", " & y
                # my dlog(image_info)
            end tell
        on error error_message
            display dialog error_message
        end try
    end if
end repeat

set myfile to open for access (path to desktop as text) & "image_data.txt" with write permission
write image_info to myfile
close access myfile

Finally, you can improve the performance of your code by switching from enumerating files in the Finder context to enumerating in the System Events context:

If you substitute the following two lines for their current counterparts in the code above, your script will run much more quickly. Why, you ask? Again, I do not know.

  • tell application "System Events" to repeat with thisFilePath in alias (the_folder as text)
  • set this_image to open (get path of thisFilePath)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top