Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 03-21-2004, 06:08 PM   #1
Guybrush
Triple-A Player
 
Join Date: May 2003
Location: homeless
Posts: 111
Applescript strcmp?

i never made something in applescript, but i wanted to make a applescript folder action for the desktop.

the script would automatically convert the "screencapture's" ouput (Picture 1.pdf, Picture 2.pdf, etc) to PNG's.

something similiar to the Duplicate as PNG action script.

but i dont know how to check if the filename has the format of "Picutre #.pdf", anyone knows?
Guybrush is offline   Reply With Quote
Old 03-21-2004, 06:26 PM   #2
huskerchad
Major Leaguer
 
Join Date: Feb 2004
Posts: 278
You could manually dig through the string yourself, using constructs like "characters 1 through n of thestring". Here is a reference for some of those kind of commands.

A more powerful and probably easier solution is to use a regex scripting addition, like this one.

You might also try something from the terminal. "screencapture" is the command-line interface, and "screencapture -c" will put the result on the clipboard. You could then use "pbpaste" and pipe that pdf to an image conversion program to get png output.
huskerchad is offline   Reply With Quote
Old 03-21-2004, 06:47 PM   #3
Guybrush
Triple-A Player
 
Join Date: May 2003
Location: homeless
Posts: 111
thanks, i will read that documentation tomorrow

the reason i want it to do it this way is that i have bound the builtin screencapture command to a different key, namely to apple+option+F12 on my powerbook (more accessible and memorizeable than the default one, apple+shift+3 )

copy pasting it sounds overkill, and not very efficient

but ofcourse the best thing would be if Apple made an option to specify the screencapture output. I dont get it why they didnt, really.

Maybe we should go bug Apple for this feature, who's with me?
Guybrush is offline   Reply With Quote
Old 03-21-2004, 07:55 PM   #4
Lankhmart
Triple-A Player
 
Join Date: May 2003
Posts: 150
You could use the folder action script that I posted to this thread and just replace every instance of JPEG and jpg with png. This should do it:

Code:
on adding folder items to this_folder after receiving these_items
	repeat with an_item in these_items
		tell application "Finder"
			set file_name to name of an_item
			set file_ext to name extension of an_item
			if file_name starts with "Picture" and file_ext is "pdf" then
				set png_path to my getSafeName()
				tell me to convertToPNG(an_item, png_path)
			end if
		end tell
	end repeat
end adding folder items to

on getSafeName()
	set desk_path to (path to desktop as Unicode text)
	try
		alias (desk_path & "Picture 1.png")
		set i to 0
		repeat
			set i to i + 1
			set path_to_check to (desk_path & "Picture " & i & ".png")
			try
				alias path_to_check
			on error
				set png_path to path_to_check
				exit repeat
			end try
		end repeat
	on error
		set png_path to (desk_path & "Picture 1.png")
	end try
	return png_path
end getSafeName

on convertToPNG(pdf_file, png_path)
	tell application "Image Events"
		launch
		set image_ref to open pdf_file
		save image_ref in png_path as PNG with icon
		close image_ref
		delete pdf_file
	end tell
end convertToPNG
Just attach that folder action to your desktop and any screen shots you make should automatically convert to PNG. (Note: this script deletes the original PDF file; if you want to keep it, remove the line "delete pdf_file" from the convertToPNG handler.)
Lankhmart is offline   Reply With Quote
Old 03-21-2004, 08:24 PM   #5
Guybrush
Triple-A Player
 
Join Date: May 2003
Location: homeless
Posts: 111
Thumbs up THANKS!

Cool, now i dont have to figure out applescript

The script works like a charm, thanks mate!
Guybrush is offline   Reply With Quote
Old 03-22-2004, 01:09 AM   #6
Lankhmart
Triple-A Player
 
Join Date: May 2003
Posts: 150
You're welcome. I'm glad it did the job.
Lankhmart is offline   Reply With Quote
Old 08-10-2004, 07:47 PM   #7
alanwatts
Prospect
 
Join Date: Aug 2004
Posts: 3
Quote:
Originally Posted by Lankhmart
You could use the folder action script that I posted to this thread and just replace every instance of JPEG and jpg with png. This should do it:

Here's another version that lets you choose the file format to convert it to:

Code:
on adding folder items to this_folder after receiving these_items
	repeat with an_item in these_items
		tell application "Finder"
			set file_name to name of an_item
			set file_ext to name extension of an_item
			set desk_path to (this_folder as string)
			
			set pdf_size to (size of file an_item as integer)
			repeat until pdf_size > 0
				delay 1
				set pdf_size to (size of file an_item as integer)
			end repeat
			
			if file_name starts with "Picture" and file_ext is "PDF" then
				set format_return to display dialog "Select output format." buttons {".pct", ".jpg", ".pdf"} default button ".pdf"
				set output_format to button returned of format_return
				set AppleScript's text item delimiters to "."
				set base_filename to the first text item of file_name
				set img_path to desk_path & base_filename & output_format
				tell me to convert(an_item, img_path, output_format)
			end if
		end tell
	end repeat
end adding folder items to

on convert(img_file, img_path, output_format)
	tell application "Image Events"
		launch
		set image_ref to open img_file
		if output_format is ".jpg" then
			save image_ref in img_path as JPEG with icon
			delete img_file
		else if output_format is ".pct" then
			save image_ref in img_path as PICT with icon
			delete img_file
		end if
		close image_ref
	end tell
end convert
Alan
alanwatts is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 08:29 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.