You are welcome. I’m glad someone else has found this useful!
Below is an amended version of the script. I have broken the last ‘do shell script’ line into the individual lines you require.
set iFolder to (choose folder with prompt "Select Folder")
set fPath to POSIX path of iFolder
set fContent to list folder iFolder as list without invisibles
repeat with i from 1 to number of items in fContent
set source_file to item i of fContent
set fName to do shell script "basename " & quoted form of POSIX path of source_file
set type to (do shell script "echo " & fName & " | cut -d'.' -f2")
set wholePath to fPath & source_file
try
do shell script "touch ~/Desktop/results.txt"
end try
try
-- sips does not work on eps files so we have to convert to pdf, save as a temp file on the Desktop, then delete it (by default this will create a 150dpi pdf!)
if type contains "eps" then
set epsSaved to do shell script "/usr/bin/pstopdf " & quoted form of wholePath & " -o ~/Desktop/tmp.pdf"
set Res to paragraphs of (do shell script "sips -g dpiHeight -g dpiWidth ~/Desktop/tmp.pdf | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
set dimensions to paragraphs of (do shell script "sips -g pixelHeight -g pixelWidth ~/Desktop/tmp.pdf | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
delay 0.1
do shell script "rm ~/Desktop/tmp.pdf"
set Res to round Res
else
set Res to paragraphs of (do shell script "sips -g dpiHeight -g dpiWidth " & quoted form of wholePath & " | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
--end if
set Res to round Res
set dimensions to paragraphs of (do shell script "sips -g pixelHeight -g pixelWidth " & quoted form of wholePath & " | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
end if
set dpi_to_dpmm to Res / 25.4 -- converts dots per inch to dots per mm
set h to (item 1 of dimensions) --/ dpi_to_dpmm -- works out pixel height as mm
set w to (item 2 of dimensions) --/ dpi_to_dpmm -- works out pixel width as mm
end try
do shell script "echo 'File Name: " & fName & " ' >> ~/Desktop/results.txt"
do shell script "echo 'Resolution: " & Res & "' >> ~/Desktop/results.txt"
do shell script "echo 'Dimensions: " & h & "px h x " & w & "px w' >> ~/Desktop/results.txt"
do shell script "echo ' ' >> ~/Desktop/results.txt"
end repeat
I must admit, I am in awe of what can be achieved with Applescript and Unix commands!
Keep scripting.
edit: I have removed the (Approx!) from the end of the Dimensions as this does return the actual pixels.
As for adding the actual image to the text file, I’m not sure you can do this with a plain text file (which is how the shell is writing to it!)
You could probably to do this with TextEdit ? ? ?
I have learnt a great deal about Apple script from this website! There are lots of tutorials in the unScripted section.
I also remember that the AS4AS document helped a lot and is something that I still refer to from time to time. It can be downloaded from http://macscripter.net/viewtopic.php?id=24599here.
I’ve been thinking how you could achieve what you originally asked for.
If you build a html file rather than a txt file, you can then open this in a browser, then select all and copy and paste into a text document. So long as your original images are not too large this may be a workable solution:-
try
do shell script "rm ~/Desktop/pix/results.html"
end try
-- set html head and body open
do shell script "echo \"<html>\" >>~/Desktop/pix/results.html"
do shell script "echo \"<head>\" >>~/Desktop/pix/results.html"
do shell script "echo \"<title>selected_images</title>\" >>~/Desktop/pix/results.html"
do shell script "echo \"</head>\" >>~/Desktop/pix/results.html"
do shell script "echo \"<body>\" >>~/Desktop/pix/results.html"
set iFolder to (choose folder with prompt "Select Folder")
set fPath to POSIX path of iFolder
set fContent to list folder iFolder as list without invisibles
repeat with i from 1 to number of items in fContent
set source_file to item i of fContent
set fName to do shell script "basename " & quoted form of POSIX path of source_file
set type to (do shell script "echo " & fName & " | cut -d'.' -f2")
set wholePath to fPath & source_file
try
-- sips does not work on eps files so we have to convert to pdf, save as a temp file on the Desktop, then delete it (by default this will create a 150dpi pdf!)
if type contains "eps" then
set epsSaved to do shell script "/usr/bin/pstopdf " & quoted form of wholePath & " -o ~/Desktop/tmp.pdf"
set Res to paragraphs of (do shell script "sips -g dpiHeight -g dpiWidth ~/Desktop/tmp.pdf | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
set dimensions to paragraphs of (do shell script "sips -g pixelHeight -g pixelWidth ~/Desktop/tmp.pdf | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
delay 0.1
do shell script "rm ~/Desktop/tmp.pdf"
set Res to round Res
else if type contains "html" then
set fName to "this_is_the_html_file"
set Res to "n/a"
set h to "n/a"
set w to "n/a"
else
set Res to paragraphs of (do shell script "sips -g dpiHeight -g dpiWidth " & quoted form of wholePath & " | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
--end if
set Res to round Res
set dimensions to paragraphs of (do shell script "sips -g pixelHeight -g pixelWidth " & quoted form of wholePath & " | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
end if
set dpi_to_dpmm to Res / 25.4 -- converts dots per inch to dots per mm
set h to (item 1 of dimensions) --/ dpi_to_dpmm -- works out pixel height as mm
set w to (item 2 of dimensions) --/ dpi_to_dpmm -- works out pixel width as mm
end try
do shell script "echo \"<img src=file:///Users/yourusername/Desktop/pix/" & fName & " width=\"25%\" height=\"25%\"><br>\">>~/Desktop/pix/results.html" --change yourusername to your actual User Name!!
do shell script "echo \"File Name = \"" & fName & "\"<br>\" >>~/Desktop/pix/results.html"
do shell script "echo \"Resolution = \"" & Res & "\"<br>\" >>~/Desktop/pix/results.html"
do shell script "echo \"Dimensions = \"" & h & " px x\"" & w & " px <br>\" >>~/Desktop/pix/results.html"
end repeat
-- close the html body (out of the repeat routine!)
do shell script "echo \"</body>\" >>~/Desktop/pix/results.html"
do shell script "echo \"</html>\" >>~/Desktop/pix/results.html"
To use this you will have to of created a folder at your Desktop called ‘pix’, then place all of your images into this folder (add your username where commented in the script). Run the script and when prompted, select this ‘pix’ folder. The script will then generate a file called ‘results.html’ within this folder. Open this ‘results.html’ file in a browser. You should be able to select all and paste into a blank text/word document. Even if images do not display in the browser, they do seem to come over when pasted into a text document.
This is probably a little messy and could, no doubt be cleaned up a lot!
i don’t know what to say but again, Thank You, and it’s exactly what I wanted. This really is brilliant. But yet again I feel very embarrassed to as one more time, what is the best way to bring the images in at actual size? I did reduce them to 10% but they seem to be contained inside a square ratio.
Are you based in the UK, I feel I must somehow return the complement.
do shell script "echo \"<img src=file:///Users/yourusername/Desktop/pix/" & fName & " width=\"25%\" height=\"25%\"><br>\">>~/Desktop/pix/results.html" --change yourusername to your actual User Name!!
with:
do shell script "echo \"<img src=file:///Users/yourusername/Desktop/pix/" & fName & " width=\"25%\"><br>\">>~/Desktop/pix/results.html" --change yourusername to your actual User Name!!
I’ve just deleted the ‘height=25%’ from the html. I didn’t realise that defining just the width would maintain the original aspect ratio! Of course, to bring them in at 100% either change the 25% to 100% or just delete the [width="25%"] part from this line.
As mentioned in my previous post, this will only affect the view of these images in the browser and when/if copied to a text file will come in at 100%.
I am based in the UK and the greatest complement is the fact that you have found this useful!
All working perfectly and thank you a great applescript to have. Just typical with clients, give them something that makes life easy for them and they want to change it around.
set source_file to choose file
set type to (do shell script "GetFileInfo " & quoted form of POSIX path of source_file & " | cut -d'\"' -f2")
When I ran it I got this log report :
tell application “AppleScript Editor”
choose file
→ alias “Macintosh HD:Users::Documents:tempo:2012-04-19T18.42.51.png”
end tell
tell current application
do shell script “GetFileInfo ‘/Users//Documents/tempo/2012-04-19T18.42.51.png’ | cut -d’"’ -f2”
→ “”
end tell
which seems a bit annoying.
In a more recent one I found :
set source_file to choose file
set fName to do shell script "basename " & quoted form of POSIX path of source_file
set type to (do shell script "echo " & fName & " | cut -d'.' -f2")
Create a new post and I’ll take a look. That way, if anyone else can answer it you may get several solutions!
@Yvan,
The “cut -d” part of the shell script will define a delimiter. In you first example there is no "" so asking for the second item (-f2) will result in “” (nothing).
The second example uses the “.” as a delimiter and was used on the understanding/assumption that a file name would only contain 1 full point always at the end of the filename, followed by the filetypes extension (e.g. “.jpg” “.doc”). Your example has “.” within the filename too.
I don’t claim to be an expert in either Unix or (especially not) Applescript. If there is a better or easier way the same results could be achieved (I’m sure there is!) I am more than happy to learn.
I am also aware that, while all of the scripts I have written do work for me on my system, they may well not work on multiple systems with different OS’s and set-ups. In fact, what is the best way to ensure any script/app is universally useable?
I tried to highlight the fact that the script fails when the pictures treated are screenshots created by the good old cmd + shift + 4 call which include the time with the format hh.mm.ss.
I would not trigger a shell script to get the name extension.
version #1
set source_file to choose file
tell application "System Events" to tell disk item (source_file as text)
set fName to name
name extension
end tell
set type to result
version #2 using Shane Stanley’s free ASObjC Runner.app
set source_file to choose file
tell application "ASObjC Runner" to (about file source_file)
set {fName, type} to {name, name extension} of result
Yvan KOENIG (VALLAURIS, France) vendredi 20 avril 2012 14:25:13
Version #1 returns JUST the extension (I’m guessing Version #2 does the same).
How would you get the filename WITHOUT the extension and without having to open it? (eg “Untitled.txt” as just “Untitled”)
Also, when I do “cmd + shift + 4”, my files are saved just as Picture 1.png. I am sure this is a user preference too.
Coming from a ‘Repro’ background and having used an old Sun ‘Sparc’ workstation to scan images to, I guess I have inherited the strict naming convention where you could not even use spaces in file names (using the underscore instead!)
Getting the bare file name doesn’t need that we open the file. It’s a trivial task.
set source_file to choose file
tell application "ASObjC Runner" to (about file source_file)
set {fName, type} to {name, name extension} of result
if type is "" then
set bareName to fName
else
set bareName to text 1 thru -((count of type) + 2) of fName
end if
set source_file to choose file
tell application "System Events" to tell disk item (source_file as text)
set fName to name
name extension
end tell
set type to result
if type is "" then
set bareName to fName
else
set bareName to text 1 thru -((count of type) + 2) of fName
end if
If you read carefully youn will see that I didn’t defined the variable type in the block tell application “System Events” because type is a property defined in this application’s dictionary.
This is why I wrote that naming a variable « type » is a bad choice.
Yvan KOENIG (VALLAURIS, France) vendredi 20 avril 2012 21:44:37
set f to POSIX path of (choose file)
set beg to current date
repeat 1000 times
repeat 1000 times
set justName to do shell script "basename " & quoted form of f & " | rev | cut -d'.' -f2-20 | rev"
end repeat
end repeat
set fin to current date
fin - beg
-->10262
Test #2
set f to POSIX path of (choose file)
set beg to current date
repeat 1000 times
repeat 1000 times
tell application "System Events" to tell disk item (f as text)
set fName to name
name extension
end tell
set type to result
if type is "" then
set bareName to fName
else
set bareName to text 1 thru -((count of type) + 2) of fName
end if
end repeat
end repeat
set fin to current date
fin - beg
--> 3898
My « long » script run 2.6 times faster than your « short » one
Now I’m waiting for an enhanced version of ASObjC Runner delivering the bareName among other properties.
Yvan KOENIG (VALLAURIS, France) lundi 23 avril 2012 17:00:07
The long time was deliberate.
I ran the test under 10.7.3
When your script was in use, I wasn’t in front of the mac so, only the script was at work.
When I ran mine, I was busy on the machine
Calling System Events always introduce an important extraneous time.
This is why some of us choose to use OSAXen like Satimage or, even better for available features, the late release of Shane Stanley’s ASObjC Runner.app
On my machine, it’s ran once during the startup process so it’s immediately available when a script must use one of its features.
Yvan KOENIG (VALLAURIS, France) lundi 23 avril 2012 19:22:22
It’s possible, although this case seems extreme. My (Lion) figures with your shorter tests show the shell method taking about 1.5 times as long. But there are lots of variables in these sorts of tests (and the time taken for System Events to launch).
There is one thing to keep in mind, though: size of code has no bearing on speed of execution. So this method will be considerably quicker than either of the others, by a large factor:
set f to POSIX path of (choose file)
set beg to current date
repeat 10 times
repeat 100 times
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
set theName to text item -1 of f
set AppleScript's text item delimiters to {"."}
set bareName to text 1 thru text item -2 of theName
set AppleScript's text item delimiters to saveTID
end repeat
end repeat
set fin to current date
fin - beg
I have to agree Shane, Speed is not everything and thinking about it, the speed at which each of these scripts get the “basename” less extension on one file, is probably negligible. Having said that, your “text item delimiter” version, even repeating 100 times (twice), is way faster. Worth baring in mind for future projects, especially when ‘batch’ processing.