Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 14, 2012, 10:28 AM   #1
o0olliw
macrumors newbie
 
Join Date: Nov 2012
Applescript help for a beginner: Trying to square and modify image dimensions

Hi there,

I'm new to this forum and also generally new to Applescript. What I've been trying to do is to create an application to 1) Square a bunch of images and save them to a folder and then 2) Load those squared images and resize them into various different dimensions. Here is my code:

Code:
-----------------------------------------------------------
-----------------------------------------------------------
-- save in Script Editor as Application
-- drag files to its icon in Finder
property save_folder : "Users:username:Desktop"
property type_list : {"TIFF", "JPEG", "PNGf", "PICT"}
property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "pct"}

on open some_items
	do shell script "mkdir -p '/Users/username/Desktop/ImageResizing'"
	do shell script "mkdir -p '/Users/username/Desktop/ImageResizing/Squared'"
	do shell script "mkdir -p '/Users/username/Desktop/ImageResizing/750/'"
	
	repeat with this_item in some_items
		try
			make_square(this_item)
		end try
	end repeat
	
	
	tell application "Finder"
		display dialog "Telling app finder"
		set source_folder to "Macintosh HD:Users:username:Desktop:ImageResizing:Squared:" as alias
		set a_list to every file in source_folder as alias list
		
		repeat with i from 1 to number of items in a_list
                        display dialog a_list
			set a_file to (item of a_list)
			rescale_and_save_750(a_file)
		end repeat
	end tell
	
end open


to make_square(this_item)
	tell application "Finder"
		set folder_path to "Users:username:Desktop:ImageResizing:Squared:"
		set file_path to (folder_path)
	end tell
	
	tell application "Image Events"
		launch
		-- open the image file
		set this_image to open this_item
		
		set typ to this_image's file type
		
		copy dimensions of this_image to {current_width, current_height}
		if current_width is greater than current_height then
			set pad_dimensions to {current_width, current_width}
			pad this_image to dimensions pad_dimensions with pad color {65535, 65535, 65535}
		else if current_height is greater than current_width then
			set pad_dimensions to {current_height, current_height}
			pad this_image to dimensions pad_dimensions with pad color {65535, 65535, 65535}
		end if
		
	end tell
	
	save this_image in file_path as typ
end make_square


to rescale_and_save_750(this_item)
	display dialog "I'm in 750"
	tell application "Finder"
		display dialog "I'm in 750"
		set folder_path to "Users:username:Desktop:ImageResizing:750:"
		set file_path to (folder_path)
	end tell
	
	tell application "Image Events"
		launch
		
		set the target_width to 750
		-- open the image file
		set this_image to open this_item
		
		set typ to this_image's file type
		
		set the target_width to 750
		set the target_height to 750
		
		copy dimensions of this_image to {current_width, current_height}
		if current_width is greater than target_width then
			scale this_image to size target_width
		else if current_height is greater than target_height then
			scale this_image to size target_width
		end if
	end tell
	
	save this_image in file_path as typ
	
end rescale_and_save_750
-----------------------------------------------------------
-----------------------------------------------------------
The place where I'm running into a problem is some where around these lines: "set a_list to every file in source_folder as alias list
repeat with i from 1 to number of items in a_list
display dialog a_list
set a_file to (item of a_list)".

It seems that the function "rescale_and_save_750" is never actually entered because the dialogue in that function never displays. I am getting an error that says "Can't make {alias "file name and path"} into type string." Does anybody know what exactly I'm doing wrong? I know that the resize to 750 function itself and the square function itself are both working seamlessly as I tried them individually and they worked. Again, I'm a newbie at this and would REALLY appreciate any help.

--Will

Last edited by maflynn; Nov 14, 2012 at 11:47 AM.
o0olliw is offline   0 Reply With Quote
Old Nov 14, 2012, 05:41 PM   #2
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Quote:
It seems that the function "rescale_and_save_750" is never actually entered because the dialogue in that function never displays. I am getting an error that says "Can't make {alias "file name and path"} into type string."
The error is caused by the display dialog a_list statement. Display dialog displays text and a_list is a list. There's also a couple of errors regarding specifying paths ,calling handlers and some other things.

Quote:
Specifying Paths
You can create alias objects and file objects by supplying a name specifier, where the name is the path to an item in the file system.

For alias and file specifiers, the path is an HFS path, which takes the form "disk:item:subitem:subsubitem:...:item". For example, "Hard_Disk:Applications:Mail.app" is the HFS path to the Mail application, assuming your boot drive is named "Hard_Disk".

Quote:
Calling Handlers in a tell Statement
To call a handler from within a tell statement, you must use the reserved words of me or my to indicate that the handler is part of the script and not a command that should be sent to the target of the tell statement.

For example, the following script calls the minimumValue handler defined in “Handlers with Positional Parameters” from within a tell statement. If this call did not include the words of me, it would cause an error, because AppleScript would send the minimumValue command to TextEdit, which does not understand that message.

tell front document of application "TextEdit"
minimumValue(12, 400) of me
set paragraph 1 to result as text
end tell
--result: The handler call is successful.
Instead of using the words of me, you could insert the word my before the handler call:

my minimumValue(12, 400)
Tips : There's no need for 3 do shell script statements, a modified one should do the trick.
Code:
-- Macintosh HD:Users:username:Desktop: can be written as below statement
(path to desktop folder as text)
More info : https://developer.apple.com/library/...0983-CH208-SW1
http://macscripter.net/viewtopic.php?id=25631
__________________
Space Corps Directive 34124

Last edited by kryten2; Nov 14, 2012 at 06:20 PM. Reason: Added tips
kryten2 is offline   0 Reply With Quote
Old Nov 15, 2012, 08:54 AM   #3
o0olliw
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Thanks for the reply. I really appreciate the help!

I removed the "display dialogue a_llist" statement, and also I modified calling the handler to include "my" in front of it since it's inside a tell statement. As far as the HFS path goes, I am already doing it with the "Macintosh HD:Users:etc" I presume. The Macintosh HD is my disk.

In all, the changes fixed some things, but now I am getting a different error. I can tell that the folder path is correct at least because it is printing out the names of the images within the "Squared folder". However, the error says: "Can't get item { name of items } of application Finder". I have narrowed down where the error takes place and I'm sure it's within these few lines:

Code:
set a_list to every item in source_folder	
		repeat with i from 1 to number of items in a_list
			set a_file to (item of a_list)
			my rescale_and_save_750(a_file)
		end repeat
Any more help/thoughts would be much appreciated!

Thanks,
--Will
o0olliw is offline   0 Reply With Quote
Old Nov 15, 2012, 10:30 AM   #4
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Try this :


Code:
		repeat with a_file in a_list
			display dialog (a_file as text)
			my rescale_and_save_750(a_file)
		end repeat
Working example droplet, not everything is corrected or changed.
Attached Files
File Type: zip Testsquaredapp.zip (27.4 KB, 16 views)
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Nov 16, 2012, 08:58 AM   #5
o0olliw
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Thanks for the reply and the example code. I used the application you created and although there were no errors, it's still not working. The "rescale_and_save_750" function is resizing the original images and not the squared images even though the source folder is the newly created squared folder. Do you know why this is?

Again, thanks so much for the help.

--Will
o0olliw is offline   0 Reply With Quote
Old Nov 16, 2012, 01:36 PM   #6
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Must have been tired yesterday. Try this droplet :
Attached Files
File Type: zip Testsquaredapp2.zip (25.1 KB, 7 views)
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Nov 16, 2012, 04:04 PM   #7
o0olliw
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Still not working :-/ Despite the changes, it doesn't take the newly squared photos and resize them, it still takes the original photos. Your help is so, so appreciated though.

--Will
o0olliw is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Square image previews ? Richard56 OS X 10.8 Mountain Lion 1 Jan 9, 2014 05:45 AM
Best way to get image dimensions? ArtOfWarfare iPhone/iPad Programming 14 Sep 15, 2013 06:14 PM
Request For GTX680 4GB modify rom image patlee88 Mac Pro 15 Jul 24, 2013 06:57 PM
How to modify rotating desktop image options? jdiamond Mac Basics and Help 1 Mar 22, 2013 04:55 AM
image dimensions missing robm2002 Mac OS X 10.7 Lion 1 Oct 9, 2012 10:59 PM

Forum Jump

All times are GMT -5. The time now is 07:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC