make a phone call with apple script or automator WITHOUT dialogue box

Hello,

I’ve been using this script below and testing it,

I’m a complete amateur when it comes to scripting, and not sure which bit i need to change,

but i was wondering if their was a method, or rewriting it, so that it doesn’t display the dialog,

but, will call someone instantly depending on who i input into the script ? i.e.,

if i was to input my partners number, or home number, or an email FaceTime address, could it call instantly upon pressing the script button without the dialog box appearing ?

many thanks in advance,

stuart

set phone_num to text returned of (display dialog “Input a phone number to call:” default answer “”)
do shell script “open facetime://” & quoted form of phone_num
tell application “System Events”
repeat while not (button “Call” of window 1 of application process “FaceTime” exists)
delay 1
end repeat
click button “Call” of window 1 of application process “FaceTime”
end tell

You may try :

# Assumes that the wanted phone number is selected in an application

tell application (path to frontmost application as string)
	set phone_num to the selection
end tell

do shell script "open facetime://" & quoted form of phone_num
tell application "System Events" to tell process "FaceTime"
	set frontmost to true
	tell window 1
		repeat while not (button "Call" exists)
			delay 1
		end repeat
		click button "Call"
	end tell
end tell

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) dimanche 14 mai 2017 20:21:51

Hello,

thank you for your quick reply !

i pasted the script in, but i received a ‘script error’,

im not sure where id need to enter the contacts email address or phone number

many thanks again

stuart

Just replace the first block of the suggested script with the first line of your original. Yvan’s version was designed to be used with a phone number already visible and selected in an application (but I’m not sure that actually worked).

set phone_num to text returned of (display dialog "Input a phone number to call:" default answer "")
do shell script "open facetime://" & quoted form of phone_num
tell application "System Events" to tell process "FaceTime"
	set frontmost to true
	tell window 1
		repeat while not (button "Call" exists)
			delay 1
		end repeat
		click button "Call"
	end tell
end tell

The first time you run the script, you may be prompted to give the script permission to control the app.

As pointed by emendelson, my script assumed that the application in which the phone number is selected is able to pass its selection.
In fact , using tell application (path to frontmost application as string) was a wrong scheme.

As I have some folders named according to phone numbers I tested and got correct result with :

tell application "System Events" -- get frontmost process
	set frontmostProcess to first process where it is frontmost -- this will be the script process
	if name of frontmostProcess is in {"Script Editor", "AppleScript Editor"} then
		set visible of frontmostProcess to false -- hide the script process
		repeat while (frontmostProcess is frontmost) -- wait until the script is hidden
			delay 0.1
		end repeat
		set theApp to name of first process where it is frontmost -- get name of frontmost process (ignoring the script process)
		set frontmost of frontmostProcess to true -- unhide the script process
	else
		set theApp to name of frontmostProcess
	end if
end tell
tell application theApp
	activate
	set phone_num to the selection
	if theApp is "Finder" then set phone_num to name of (phone_num as alias)
	--> "04 56 78 90 12"
end tell


do shell script "open facetime://" & quoted form of phone_num
tell application "System Events" to tell process "FaceTime"
	set frontmost to true
	repeat until exists window 1
		delay 0.1
	end repeat
	# Get the localized name of the button "Call"
	tell application "FaceTime" to set Call_loc to localized string "Call"
	tell window 1
		--name of buttons --> {"Annuler", "Appeler", missing value, missing value, missing value}
		click button Call_loc
	end tell
end tell

With a simple modification the script does the trick for application which doesn’t recognize selection.
It copy the selected string in the clipboard then grab it.

 tell application "System Events" -- get frontmost process
	set frontmostProcess to first process where it is frontmost -- this will be the script process
	if name of frontmostProcess is in {"Script Editor", "AppleScript Editor"} then
		set visible of frontmostProcess to false -- hide the script process
		repeat while (frontmostProcess is frontmost) -- wait until the script is hidden
			delay 0.1
		end repeat
		set theApp to name of first process where it is frontmost -- get name of frontmost process (ignoring the script process)
		
		keystroke "c" using {command down}
		set frontmost of frontmostProcess to true -- unhide the script process
	else
		set theApp to name of frontmostProcess
	end if
end tell
delay 0.2
set phone_num to the clipboard as text
if theApp is "Finder" then tell application theApp to set phone_num to name of (phone_num as alias)
--> "04 56 78 90 12"

do shell script "open facetime://" & quoted form of phone_num
tell application "System Events" to tell process "FaceTime"
	set frontmost to true
	repeat until exists window 1
		delay 0.1
	end repeat
	# Get the localized name of the button "Call"
	tell application "FaceTime" to set Call_loc to localized string "Call"
	tell window 1
		--name of buttons --> {"Annuler", "Appeler", missing value, missing value, missing value}
		click button Call_loc
	end tell
end tell

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) lundi 15 mai 2017 16:03:00

As the above script works very well, how might applescript:

  1. Avoid GUI scripting, altogether, by utilizing some form of open location such as in:
open location "tel://" & PhoneNumber & "?audio=yes"

The above open location attempt, by itself, fails without another user interface command to click a button.

  1. Incorporate phone number data detectors similar to Automator’s parameters

Regarding data detectors, the following objective C produces an array with a phone number.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set PhoneString to "call me at  8007654321"
my PhoneDataDetect:PhoneString

on PhoneDataDetect:PhoneString
	set anNSString to current application's NSString's stringWithString:PhoneString
	set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypePhoneNumber) |error|:(missing value)
	set theURLsNSArray to v matchesInString:PhoneString options:0 range:{0, anNSString's |length|()}
	theURLsNSArray's firstObject()
end PhoneDataDetect:

And returns


How can extract the phone number from the theNSDataDetector response?

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set PhoneString to "call me at  8007654321"
my PhoneDataDetect:PhoneString

on PhoneDataDetect:PhoneString
	set anNSString to current application's NSString's stringWithString:PhoneString
	set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypePhoneNumber) |error|:(missing value)
	set theResults to theNSDataDetector's matchesInString:anNSString options:0 range:{0, anNSString's |length|()}
	return theResults's valueForKey:"phoneNumber"
end PhoneDataDetect:

In Shane’s code, if you replace :

my PhoneDataDetect:PhoneString

by

(my PhoneDataDetect:PhoneString) as string

you will get the phone number as string : “8007654321”

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 11 janvier 2019 11:30:33

Yvan,

That’s correct. As written, the script returns a list so it can be used on a string containing multiple phone numbers.