Conditional Values (Automator)

Apart from other questions asked by many people, I'm trying to add an If/Then/Else action on an application. Here's what I'm trying to do.

This is where the problem takes place. I'm trying to add actions that can only be executed if a certain button on the dialog is selected. For example, if you select "Audio File", the application will ask you for a finder item. Otherwise, if you select "Text to Speech", the application will ask you for text, which would be spoken. Please note that I'm trying to add conditional values on an application, not a workflow.

Posted on Jul 2, 2021 1:38 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 2, 2021 3:52 PM

Automator has no control logic. Data flows into and out of each action. If you use a Run AppleScript or Run Shell Script action, you can use language-specific control logic to determine the flow of processing within that particular action, and what data flows out of it.


Button response is the purview of AppleScript where you can get the name of the button clicked in the dialog and form your logic around that result. Here is some sample code for capturing the name of the button and testing for it in a control block:


set target_kind to button returned of (display dialog "Select icon target: " buttons {"Cancel", "Folder", "File"} default button "File" cancel button "Cancel")

try
	if target_kind contains "File" then
		set targetFile to POSIX path of (choose file with prompt target_msg1 default location target_dl without invisibles)
	else if target_kind contains "Folder" then
		set targetFile to POSIX path of (choose folder with prompt target_msg2 default location target_dl)
	end if
on error errmsg number errnbr
	-- because the cancel button was selected
	my error_handler(errnbr, errmsg)
	return
end try

set istatus to my set_icon(theImage, targetFile) as boolean


It may be useful to have access to the online AppleScript Language Guide. Display dialogs are discussed in the Command References section.

1 reply
Question marked as Top-ranking reply

Jul 2, 2021 3:52 PM in response to GabeTheCoder

Automator has no control logic. Data flows into and out of each action. If you use a Run AppleScript or Run Shell Script action, you can use language-specific control logic to determine the flow of processing within that particular action, and what data flows out of it.


Button response is the purview of AppleScript where you can get the name of the button clicked in the dialog and form your logic around that result. Here is some sample code for capturing the name of the button and testing for it in a control block:


set target_kind to button returned of (display dialog "Select icon target: " buttons {"Cancel", "Folder", "File"} default button "File" cancel button "Cancel")

try
	if target_kind contains "File" then
		set targetFile to POSIX path of (choose file with prompt target_msg1 default location target_dl without invisibles)
	else if target_kind contains "Folder" then
		set targetFile to POSIX path of (choose folder with prompt target_msg2 default location target_dl)
	end if
on error errmsg number errnbr
	-- because the cancel button was selected
	my error_handler(errnbr, errmsg)
	return
end try

set istatus to my set_icon(theImage, targetFile) as boolean


It may be useful to have access to the online AppleScript Language Guide. Display dialogs are discussed in the Command References section.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Conditional Values (Automator)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.