AppleScript Image Events cant get class asDB - applescript

Wanting to learn more about Mac's Image Events I am trying to learn how to save a file from one type into another type. For example, if I have a BMP image named foobar.bmp I wanted to learn how to save it out as foobar.jpg but in my handler I get an error:
Image Events got an error: Can’t get «class asDB» id (application
"Image Events") of image "foobar.bmp".
My code inside my handler:
tell application "Finder"
set directory to (choose folder with prompt "Please select a directory.")
set someImages to (files of folder directory where name extension is "bmp") as alias list
repeat with someImage in someImages
tell application "Image Events"
launch
set workingImage to open someImage
save workingImage as JPEG in directory
close workingImage
end tell
end repeat
end tell
I did test to see if the save may need the POSIX Path instead of the Alias Path with:
set directoryPosix to (POSIX path of directory)
and changed the save:
save workingImage as JPEG in directoryPosix
but I am still producing the same error and I dont understand why. The code works but just throws an error and after searching I am unable to find a resolution. I already know how to do this with Bash using ImageMagick and I could do this using AppleScript and SIPs but I would like to learn more about Image Events. What am I doing wrong to throw the error? If it helps my OS is up-to-date and running Yosemite version 10.10.5.

You need to specify the full (HFS or POSIX) path of the new file rather than the alias specifier to the destination folder:
set directory to (choose folder with prompt "Please select a directory.") as text
tell application "Finder" to set someImages to (files of folder directory where name extension is "bmp") as alias list
tell application "Image Events"
launch
repeat with someImage in someImages
set workingImage to open someImage
set fileName to name of workingImage
set newFilePath to directory & text 1 thru -4 of fileName & "jpg"
save workingImage as JPEG in newFilePath
close workingImage
end repeat
end tell

Related

Using AppleScript to Select a File

I'm trying to get AppleScript to select a file, but I'm getting an error when I execute the script.
Here's the code
tell application "System Events"
set a to "/Users/me/files/"
set fileName to "myFile.jpg"
set thePath to POSIX path of a
tell application "Finder"
set selection to fileName of thePath
end tell
keystroke "c" using command down
end tell
I'm getting an error "Can’t get POSIX path of "/Users/me/files/"
Essentially, what I'm trying to do is find a way to select a file so that I can copy it for later. But I want to copy the actual file, not the path of the file. The idea is to create a service that copies the file so that I can paste it into another application easily.
If there's a better way to do this, then please let me know
These following 2 lines of code would copy your file to the clipboard. This would only work with a single file. Not multiple items.
activate
set the clipboard to POSIX file (POSIX path of (choose file))

Hiding all files in a user chosen directory

So basically I want to be able to take all of the files in a folder and hide them but it always stops at this point with Applescript error -1700
tell application "Finder"
set filePath to entire contents of (choose folder with
prompt "Please choose your folder") as alias list
set pFilePath to POSIX path of filePath
end tell
Can you guys help me?

Quicktime Automator + Applescript for QuickTime reference files

I'm trying to create an Automator droplet that allows me to pass one or more video files to an Applescript that then saves out reference files with "_ref.mov" appended to the file name by using QuickTime Player 7.
Saving as a reference is the default save method, which is why I'm not using save reference file function (also, because I can't find any docs on the using descriptors attribute object.)
I found this script on another forum, and it works, but it doesn't accomplish exactly what I need because it requires that 1) a file is open and 2) the user manually specifies an output file name.
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
if not (exists document 1) then display dialog "please open a quicktime movie." buttons {"cancel"} default button 1 with icon 1
set theFile to (choose file name)
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've tried to make changes to accomplish what I need, but my changes break the script:
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
open input
set theFile to ((path of input) & "_ref.mov))
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've also tried:
set theFile to (((path of input) & "_ref.mov") as text) doesn't work
either does as alias or as string
Not sure where I'm going wrong. Please help if you can!

Applescript trouble with moving files in script

this is the script I'm trying to use:
on open thisItem
set this_folder to (the POSIX path of thisItem)
set export_folder to "Macintosh HD:Users:j****.*******:Desktop:AUTOMATOR:export"
set pdf_folder to "Macintosh HD:Users:j****.*****:Desktop:AUTOMATOR:PDF"
tell application "Adobe Illustrator" to open thisItem
tell application "Adobe Illustrator" to save current document in export_folder as pdf with options {class:PDF save options, PDF preset:"Low res proofing"}
tell application "Adobe Illustrator" to close current document
tell application "UltraLowPDF" to open
tell application "Finder" to move every file of folder "Macintosh HD:Users:j*****.*****:Desktop:AUTOMATOR:PDF" to container of this_folder
end open
This script should do a few things:
1. It creates a pdf file from an .ai file using adobe illustrator.
2. It then runs that pdf file through an automator workflow, which then spits out a pdf into a folder on my desktop.
3. It then moves that pdf file from the desktop, back to the folder of the original .ai file.
Every part of the script works fine except for step 3, it just seems to ignore it entirely. I've tried adding a delay, thinking the script was getting ahead of itself.
I've also tried isolating the move part of the script, using this droplet:
on open thisItem
set this_folder to (the POSIX path of thisItem)
tell application "Finder" to move every file of folder "Macintosh HD:Users:j****.******:Desktop:AUTOMATOR:PDF" to container of this_folder
end open
But this just throws up an error saying that it can't get the «class ctnr» of the file dropped on it (-1728).
Any help is greatly appreciated.
Thanks.
(note: I've starred out the user name part of the filepath for privacy)
EDIT: It seems that it's the Automator part of the script that is the problem, (the ''UltralowPDF"" app). After this runs, nothing after it in the script will run.
The problem is that thisItem is actually theseItems (aka a list). Either you need a repeat loop or – as in the following code – get the first item of the list.
The Finder does not accept (slash separated) POSIX paths. It works only with (colon separated) HFS paths.
The script uses the relative path path to desktop which point always to the desktop of the current user.
on open theseItems
set automatorFolder to (path to desktop as text) & "AUTOMATOR:"
set thisItem to item 1 of theseItems
set export_folder to automatorFolder & "export:"
set pdf_folder to automatorFolder & "PDF:"
tell application "Adobe Illustrator" to open thisItem
tell application "Adobe Illustrator" to save current document in export_folder as pdf with options {class:PDF save options, PDF preset:"Low res proofing"}
tell application "Adobe Illustrator" to close current document
tell application "UltraLowPDF" to open
tell application "Finder" to move every file of folder pdf_folder to container of thisItem
end open

Copy mounted folders to local folder

We have a samba share from where I'd like to copy folders with an applescript. This is what I already have (the mounting works):
mount volume "smb://samba.com/e_18_data11$"
delay 3
set sourcefolder to ("smb://samba.com/e_18_data11$/e_18_data11$/folder1/folder2" as POSIX file)
set localfolder to ("/Users/username/Dropbox/Test" as POSIX file)
tell application "Finder" to duplicate sourcefolder to localfolder
This gives me still this error:
the routine can not edit objects of this class." number -10010
I tried and combined many solutions already on SO, e.g. this solution
– OS X 10.9
It's probably the sourcefolder specification that is wrong.
I think you can just use the volume name instead of "smb://".
set sourcefolder to ("/Volumes/7samba.com/e_18_data11$/e_18_data11$/folder1/folder2" as POSIX file)
(if the mounted volume is named "7samba.com")
Tip: drag the actual sourcefolder from Finder into your AppleScript. It should paste the path into the script. Use that path for sourcefolder.
More:
The Error your getting is:
Mac OS error -10010 (telBadHTypeErr): bad hook type specified
I tested it (with two local folders) to see if the script would work. It did work and duplicated the folder.
You can (or should anyway) wrap critical code into a try block, like this:
try
duplicate sourcefolder to localfolder
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try
This way you can check and react to errors.
Addition:
May be you can check for existence like this:
tell application "Finder"
set aBoolean1 to get (exists sourcefolder)
set aBoolean2 to get (exists localfolder)
end tell
log aBoolean1
log aBoolean2
Both bool's must be YES

Resources