AppleScript throws an error when creating a folder - applescript

Okay, maybe I AM stupid, but why it's happening?
Here's my code
set openedFile to (choose file with prompt "Select a file to read:" of type {"PNG"})
tell application "Finder"
set parentFolder to POSIX path of (parent of openedFile as text)
set icFolder to make new folder at parentFolder with properties {name:"resized imgs"}
end tell
And when I'm trying to run it and select a file I get this:
error "Finder got an error: AppleEvent handler failed." Number -10000
What's wrong?

The parentFolder line is causing your problems. First, there is no such command as "parent". The Finder has the command "container" to get the parent folder. Also you are messing things up using "POSIX file" and "as text". The openedFile is already in the proper format for the Finder, so you don't need to coerce it into any other form.
As such just change the parentFolder line to this. Good luck.
set parentFolder to container of openedFile

Figured it on my own:
set parentFolder to container of openedFile
set icnsFolder to (make new folder at parentFolder with properties {name:"resizedicns"}) as alias
set ic512_2x to duplicate openedFile to icnsFolder

Related

AppleScript Image Events cant get class asDB

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

Copy file with a new file name

My boss has asked me to write an AppleScript that with a file, specified with a hard coded path in the text script, Copy a file from one location in the Finder to another location (giving it a new file name in the process).
This was my idea
tell application "Finder"
copy "/blah/blahblah/
save as "blah blah2" /blah/blah/blah
end tell
Copy is an AppleScript command not a finder command,
copy 1 to a
will copy value 1 into variable a
The command you're looking for to copy files is duplicate.
set fileToCopy to "DiskName:Users:shortname:Downloads:document.pdf"
set targetFolder to "DiskName:Users:shortname:Desktop:"
tell application "Finder" to duplicate fileToCopy to folder targetFolder
Thanks for the downvotes!
Any who'zle this is what I ended up with
set desktopFolder to path to desktop as text
set thefile to desktopFolder & "lol.html"
set DestinationPath to desktopFolder & "BLAH BLAH BLAH"
set destinationFilename to "ROFL2.txt"
with timeout of 60 seconds
tell application "Finder"
try
set theDupe to duplicate file thefile to folder DestinationPath
set name of theDupe to destinationFilename
on error
display dialog "A problem occurred duplicating the file. This may be because the file already exists!"
end try
end tell
end timeout

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

Applescript error when trying to get file list from folder

I'm having trouble getting AppleScript to read a list of files from a folder without receiving the error message "Can't get every file from folder XXX"
set targetfolder to ("DICTAPHONE:DSS_FLDA:")
tell application "Finder"
set fileselection to every file in targetfolder
endtell
This worked flawlessly before upgrading to Mavericks. Paths are correct. I tried it with a different folder on my startup disk and got the same result.
Try using folder targetfolder instead of targetfolder:
set targetfolder to ("Macintosh HD:Library:Desktop Pictures")
tell application "Finder"
files of (folder targetfolder)
end tell
Try this to get the information:
set targetfolder to (path to desktop as alias)
tell application "Finder"
set fileselection to get the name of every file of targetfolder
end tell

applescript Posix chaos

Yet again flummoxed by syntax and ordering of an applescript workflow solution
When I create the following code
tell application "Finder"
set remote_p to alias (POSIX file '/Volumes/WAM XSAN/Audio/AAA)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
Everything works fine. However, I need to create the main_folder in different locations dependent on the input "client_code" and "department", So i tried this:
tell application "Finder"
set x_san to "/Volumes/WAM XSAN/"
set x_sannewpath to (x_san & department & "/" & client_code)
set x_sanfolder to POSIX file x_sannewpath
set remote_p to alias (POSIX file x_sanfolder)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
And the error comes back "Can't get "Volumes/WAM XSAN/Audio/AAA" of Application Finder"
Where am i going wrong in setting up the POSIX paths?
Please help!!!!
Try this example. I tried to model it after what you're trying to do. Note that I have moved most of the code outside the Finder tell block of code. The Finder does not know the "posix file" command. That's an applescript command, not a Finder command. Also you don't need the Finder to set variables and add strings together. We can do all of that outside the Finder.
Even though it sometimes works when you have the "posix file" command inside a Finder tell block of code, it's always best if you only tell applications to do what they know how to do. You can find what an application knows how to do by looking in its applescript dictionary. Check the Finder dictionary and you will see that "posix file" is not one of its commands.
Anyway, this will create a folder on your desktop. I hope this helps.
set x_san to "/Users/hmcshane/"
set client_code to "Desktop"
set temp_name to "test folder"
set x_sannewpath to x_san & client_code
set applescript_x_sannewpath to POSIX file x_sannewpath
tell application "Finder"
set main_folder to (make new folder at applescript_x_sannewpath with properties {name:temp_name}) as alias
end tell
Try:
set department to "Audio"
set client_code to "AAA"
set temp_name to "New Folder"
set x_san to "Volumes/WAM XSAN/"
set x_sannewpath to x_san & department & "/" & client_code
tell application "Finder" to set main_folder to (make new folder at POSIX file x_sannewpath with properties {name:temp_name})

Resources