Get XMP Rating by using AppleScript - applescript

How can I get the rating existing in an xmp file by using AppleScript.
The xmp file belongs to an CR2 file. I tried it by using the photoshop cc 2017 library. But I don't get it work.

I've always found exiftool to be handy for reading/writing XMP
http://www.sno.phy.queensu.ca/~phil/exiftool/
If you need to access it from AppleScript then you can use something like:
set theFile to choose file
set theInfoFromExiftool to do shell script "/usr/local/bin/exiftool -CreateDate " & quoted form of POSIX path of theFile
Of course, you'll need to change CreateDate to the name of whatever tag you're interested in.
Hope that's of some help.

Related

Open file in Preview

I struggle with a basic file operation in Apple Script. A pdf is (successfully) written in the script like this:
theDocument's writeToURL:targetFile
Now I want to open the file in Preview like this:
tell application "Preview"
activate
open targetFile
end tell
I also tried this:
tell application "Preview"
activate
open POSIX path of targetFile
end tell
to no avail. I somehow seem to lack an understanding how to get the path to a given file.
What am I missing?
Your code doesn’t show what targetFile is, but assuming from the first line it’s an NSURL you need to convert it to an AppleScript file type before passing it to Preview’s open command:
set targetFile to targetFile's |path|() as string as POSIX file
AppleScript’s Apple event bridge doesn’t accept ObjC classes, only native AS types.

Create apple notes with AppleScript

I get the text from the file and create a note, but the text is added to the notes without hyphenation, in one line! How to make all hyphenations be copied?
My code:
set x to read POSIX file "/files/mytext.txt"
tell application "Notes"
tell account "iCloud"
make new note at folder "Blogs" with properties {name:"My Blogs", body:x}
end tell
end tell
Thanks!
I discovered that the "Notes" application is actually using HTML format (it said so in the AppleScript Dictionary for that application), and HTML does not correctly format newlines; so that means that all of the newlines need to be replaced with <br> using the shell command awk. Reading the file using awk instead of AppleScript itself also seems to solve the hyphenation problem.
Here is the fixed command:
set x to (do shell script "awk '{printf \"%s\\<br>\", $0}' '/files/mytext.txt'")
tell application "Notes"
tell account "iCloud"
make new note at folder "Blogs" with properties {name:"My Blogs", body:x}
end tell
end tell

can i automate .doc to .htm using apple script or automator?

as i see, There is no way to convert .doc files to .htm, "convert format of Word documents" in automator, no save as .htm, and for applescript word functions
i didn't see it too(
You can do it in AppleScript:
tell application "Microsoft Word"
save as active document file name "converted.html" file format format HTML
end tell
The file name should be a path to wherever you want it created using old-style Mac paths, e.g. Macintosh HD:Users:somebody:Desktop:converted.html.
Yes. The unix command line utility "textutil" can do it. Something like this. Notice that since you're converting to html we can use the command line utility "tidy" to clean up the html code before writing to a file.
set docFile to choose file
do shell script "/usr/bin/textutil -stdout -format doc -convert html " & quoted form of POSIX path of docFile & " | /usr/bin/tidy -ib -utf8 -output " & quoted form of POSIX path of ((docFile as text) & ".html")

AppleScript : Trying to write in a TextEdit file

I'm trying to write in a TextEdit file already created.
The file is in rwxrwxrwx mode so no permission problem.
But when I execute my code, here is the error :
error "Network file permission error." number -5000 from file "/Users/me/Desktop/A directory/file.txt" to «class fsrf»
My code here :
-- Writing in the TextEdit file
set file_URLs_content to "HEEEELLOOOOOO"
tell application "TextEdit"
set theFile to "/Users/me/Desktop/A directory/file.txt"
set file_ref to (open for access file theFile with write permission)
set eof file_ref to 0
write file_URLs_content to file_ref
close access file_ref
end tell
And my file.txt is still empty, how can I avoid this error ?
The way you can avoid errors when writing text with TextEdit is to remember that it is a text editor. It already knows how to create and save text documents without generating errors. You don’t have to use (error-prone) open for access. You don’t have to use (error-prone) shell scripting. All you have to do is ask TextEdit to make you a text document with whatever contents you like and and save it wherever you like. TextEdit knows how to do that without generating file access errors (like open for access) or accidentally overwriting folders (like shell scripting.)
tell application "TextEdit"
activate
set theDesktopPath to the path to the desktop folder as text
set file_URLs_content to "HEEEELLOOOOOO"
make new document with properties {text:file_URLs_content}
save document 1 in file (theDesktopPath & "file.txt")
close document 1
end tell
The advantage of this method is it is faster and easier to write, it is less error-prone, the text file that you get as output has the same properties as text files that you create manually with TextEdit, and your script can now be easily expanded to include other apps. For example, the text content could come from another app or the clipboard, and the text file could be opened in another app or emailed after it is saved.
The most fundamental feature of AppleScript is sending messages to Mac apps in this way. If you want to convert a PNG to a JPEG, you don’t write a PNG decoder and JPEG encoder in AppleScript and open the PNG file for access and read it byte by byte and then encode a JPEG byte by byte. You simply tell Photoshop to open the PNG image and export it as a JPEG to a particular file location. The “open for access” command is a last resort for reading and writing files that you simply don’t have an app to read or write. The “do shell script” command is for incorporating command-line apps when you simply don’t have a Mac app to do the job, for example, you can do regex stuff with Perl. If all you are doing is working with text files, you not only have TextEdit, but you can also get the free TextWrangler from Mac App Store and it has a giant AppleScript dictionary for reading, writing, and editing text files.
You don't need TextEdit for that. Try this:
set the logFile to ((path to desktop) as text) & "log.txt"
set the logText to "This is a text that should be written into the file"
try
open for access file the logFile with write permission
write (logText & return) to file the logFile starting at eof
close access file the logFile
on error
try
close access file the logFile
end try
end try
Try:
set file_URLs_content to "HEEEELLOOOOOO"
set filePath to POSIX path of (path to desktop as text) & "file.txt"
do shell script "echo " & quoted form of file_URLs_content & " > " & quoted form of filePath

In Applescript how do I read a website, and output the text as a variable?

In Applescript how do I read a website, and output the text as a variable?
I want to read the "Latest" number from here, then use that number to download the latest version of Chromium from here.
Not a direct Applescript way, but this works well.
Create a shell script to do the download, say chrome-download.sh in your $HOME/bin directory:
#!/bin/sh
BUILD=`curl http://build.chromium.org/buildbot/snapshots/sub-rel-mac/LATEST`
echo "Downloading build "$BUILD
curl http://build.chromium.org/buildbot/snapshots/sub-rel-mac/$BUILD/chrome-mac.zip -o $HOME/chrome-mac-$BUILD.zip
Run it from Applescript (or Automator) in one line:
do shell script "/bin/bash /Users/<your username>/bin/chrome-download.sh"
The downloaded file lands in your $HOME directory. The bash script will work from Linux or Cygwin, as well. Of course, you could just run the bash script directly, too.
As Glenn said it's much easier to just use a shell script and possibly wrap it in AppleScript by using do shell script. Another alternative if you want a GUI of sorts to the script is to look at a program called Platypus.
Lastly if you're looking for a sample script that already does this I made one when Chromium Mac was announced a couple days back: http://chealion.ca/2009/05/chromium-build-bot-updater-script/ (Source on GitHub).
A more AppleScript-native way would be:
set filePath to (path to temporary items folder as string) & "file.html"
tell application "URL Access Scripting"
download "http://www.apple.com/sitemap/" to file filePath replacing yes
end tell
--read file into a variable
try
open for access (file filePath)
set fileData to (read file filePath)
close access (file filePath)
on error errMsg number errNum
try
close access file filePath
end try
error errMsg number errNum
end try
You can find more information in the URL Access Scripting and StandardAdditions dictionaries. (File > Open Dictionary)

Resources