For a project for children I would need a script, which allows me to select a text (let's say in word) of which every character is changed to a random font out of a pool of fonts.
Would appreciated help very much - to be honest I do not have a clue where to start.
Thank you!
Here's where to start. Apple includes on everyone's computer a bunch of pre-made applescripts. One of them is called "Crazy Message Text" and when run it asks you to type in a short message. When complete it opens a new email and types that message into the email in a variety of fonts/colors/sizes. So that would be a good place to start because it shows how to do much of what you need. Here's the script but you can find in on your computer in the /Library/Scripts/ folder. You should look through those scripts. There's a lot that can be learned in that folder.
property font_list : {"American Typewriter", "American Typewriter Light", "American Typewriter Bold", "American Typewriter Condensed", "American Typewriter Condensed Light", "American Typewriter Condensed Bold", "Arial", "Arial Italic", "Arial Bold", "Arial Bold Italic", "Arial Black", "Baskerville", "Baskerville Italic", "Baskerville SemiBold", "Baskerville Bold", "Baskerville SemiBold Italic", "Baskerville Bold Italic", "Big Caslon Medium", "Comic Sans MS", "Comic Sans MS Bold", "Copperplate", "Copperplate Light", "Copperplate Bold", "Didot", "Didot Italic", "Didot Bold", "Futura Medium", "Futura Medium Italic", "Futura Condensed Medium", "Futura Condensed ExtraBold", "Geneva", "Gill Sans", "Gill Sans Italic", "Gill Sans Light", "Gill Sans Light Italic", "Gill Sans Bold", "Gill Sans Bold Italic", "Herculanum", "Lucida Grande", "Lucida Grande Bold", "Marker Felt Thin", "Marker Felt Wide", "Optima Regular", "Optima Italic", "Optima Bold", "Optima Bold Italic", "Optima ExtraBlack", "Papyrus", "Verdana", "Verdana Italic", "Verdana Bold", "Verdana Bold Italic", "Zapfino"}
property low_color : 0
property high_color : 65535
property low_fontsize : 36
property minimum_size : 9
property high_fontsize : 72
property this_text : "Happy Birthday!"
activate
repeat
display dialog "Enter the text:" & return & return & ¬
"Minimum Character Size: " & (low_fontsize as Unicode text) & return & ¬
"Maximum Character Size: " & (high_fontsize as Unicode text) default answer this_text buttons {"Cancel", "Set Prefs", "Continue"} default button 3
copy the result as list to {user_text, button_pressed}
if the button_pressed is "Set Prefs" then
repeat
try
display dialog "Enter the minimum character size to use:" & return & return & ¬
"(Must be at least " & (minimum_size as Unicode text) & ")" default answer low_fontsize
set the user_size to text returned of the result as integer
if the user_size is greater than or equal to the minimum_size then
set low_fontsize to the user_size
exit repeat
end if
on error
beep
end try
end repeat
repeat
try
display dialog "Enter the maximum character size to use:" & return & return & ¬
"(Must be greater than " & (low_fontsize as Unicode text) & ")" default answer high_fontsize
set the user_size to text returned of the result as integer
if the user_size is greater than the low_fontsize then
set high_fontsize to the user_size
exit repeat
end if
on error
beep
end try
end repeat
else
if user_text is not "" then
set this_text to the user_text
exit repeat
end if
end if
end repeat
tell application "Mail"
activate
set this_message to make new outgoing message at end of outgoing messages with properties {content:this_text, visible:true}
tell content of this_message
repeat with i from 1 to the length of this_text
repeat
try
set this_font to some item of the font_list
set font_size to random number from low_fontsize to high_fontsize
set red_value to random number from low_color to the high_color
set green_value to random number from low_color to the high_color
set blue_value to random number from low_color to the high_color
tell character i
set font to this_font
set size to font_size
end tell
set the RGB_value to {red_value, green_value, blue_value}
set the color of character i to the RGB_value
exit repeat
end try
end repeat
end repeat
end tell
end tell
If you want to use "Microsoft Word", try this script
tell application "Microsoft Word"
set tc to count characters of selection
set fontNames to font names
set cF to count fontNames
repeat with i from 1 to tc
set thisName to item (random number from 1 to cF) of fontNames
try
tell font object of character i of selection to set name to thisName
end try
end repeat
end tell
Related
I have been working on a "notification previewer" applet for a while and I wanted to try and save notification data when the process is done.
When I added the .plist snippet of code into it I get an error that says "Cant get property list file in /~/~/~/~/~/~" (the random number at the end is a randomly generated number for the .plist).
Here is the code. Can anybody help me figure out whats wrong?
on getTimeInHoursAndMinutes()
-- Get the "hour"
set timeStr to time string of (current date)
set Pos to offset of ":" in timeStr
set theHour to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
-- Get the "minute"
set Pos to offset of ":" in timeStr
set theMin to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
--Get "AM or PM"
set Pos to offset of " " in timeStr
set theSfx to characters (Pos + 1) through end of timeStr as string
return (theHour & ":" & theMin & " " & theSfx) as string
end getTimeInHoursAndMinutes
set thetime to getTimeInHoursAndMinutes()
set ThePLISTNAME to random number from 0 to 1.0E+17
set A1 to display dialog "Enter in your title" default answer "" buttons {"Cancel", "Continue"} default button 2 with title "Notification Previewer"
set TITLE to text returned of A1
set A2 to display dialog "Enter in your text" default answer "" buttons {"Cancel", "Continue"} default button 2 with title "Notification Previewer"
set TEXT2 to text returned of A2
set A3 to display dialog "Enter in your subtitle" default answer "" buttons {"Cancel", "Continue"} default button 2 with title "Notification Previewer"
set TEXT3 to text returned of A3
set A4 to display dialog "choose a sound, LIST: Basso Blow Bottle Frog Funk Glass Hero Morse Ping Pop Purr Sosumi Submarine Tink. (CASE SENSITIVE)" default answer "" buttons {"Cancel", "Continue"} default button 2 with title "Notification Previewer"
set TEXT4 to text returned of A4
display notification TEXT2 with title TITLE subtitle TEXT3 sound name TEXT4
set textdata to {"Title: ", TITLE, "Text: ", TEXT2, "Subtitle: ", TEXT3, "Sound: ", TEXT4}
tell application "System Events"
set theParentDictionary to make new property list item with properties {kind:record, name:ThePLISTNAME}
--set fold to alias "~/Applications/Notification Previewer/Contents/Resources/Contents/Saves/"
--set TextFile to make new file at fold with properties {name:thetime, file type:"PLIST", creator type:"ttxt", type:«class fsrf»}
set thePropertyListFilePath to ("~/Applications/Notification Previewer/Contents/Resources/Contents/Saves/" & ThePLISTNAME & ".plist/")
set thePropertyListFile to make new property list file with properties {contents:theParentDictionary, name:thePropertyListFilePath}
tell property list items of thePropertyListFile
make new property list item at end with properties {kind:string, name:"stringKey", value:textdata}
end tell
end tell
What I'm trying to make is this:
set retry to true
repeat while retry is true
display dialog "In the next window you'll have to choose a color!" buttons {"Cancel", "COLORR!"} cancel button 1 default button 2
set Chosencolor1 to (choose color)
display dialog "The chosen color is: " & Chosencolor1 & " Is this right?" buttons {"NO", "YES"}
if the button returned of the result is "no" then
set retry to true
display dialog "Retry is now: " & retry
else
set retry to false
display dialog "Retry is now: " & retry
end if
end repeat
end
But when I run this code it will return a color code in numbers. But what I want is that it will return a color name like: light blue, blue, green etc.
The only way I know is to do something like:
if the chosencolor1 is "000" then
set the chosencolor1 to "Black"
end if
end
is there any other, more simple way to do this? Or is this just not possible?
Thanks for reading,
Jort
From the AppleScript Language Guide for choose color:
Result The selected color, represented as a list of three integers
from 0 to 65535 corresponding to the red, green, and blue components
of a color; for example, {0, 65535, 0} represents green.
As you have surmised, the only way to get a textual response is to program it yourself using the values of the list that your user chooses.
Here is a very simple example that adds all the values of the chosen RGB integers, and determines if it closer to black or white:
set myColor to choose color
display dialog ("You have chosen: " & my fetchColorName(myColor))
to fetchColorName(clst)
set totalColorValue to ((item 1 of clst) + (item 2 of clst) + (item 3 of clst))
set blackWhiteMidpoint to (196605 / 2)
if totalColorValue < blackWhiteMidpoint then
set colorName to "A color closer to black than white."
else
set colorName to "A color closer to white than black."
end if
return colorName
end fetchColorName
I wrote an applescript droplet where I would like to:
drag one or more images onto the droplet
3 display dialogs appear asking 'width', 'height', 'format'
process all dropped images using the above text returned of the
3 display dialogs
Currently, the 3 display dialogs appear for each image (e.g. 3 images = 9 dialogs appear). Is there a way I can only answer these dialogs once? Here's my script:
on run
display dialog "This is a droplet"
end run
on open draggedItems
set tid to AppleScript's text item delimiters
--ask for new width
set newWidth to text returned of (display dialog "New Width" default answer ¬
"45" buttons {"Continue…", "Cancel"} ¬
default button 1)
--ask for new height
set newHeight to text returned of (display dialog "New Height" default answer ¬
"45" buttons {"Continue…", "Cancel"} ¬
default button 1)
--ask for formatType
set newFormat to text returned of (display dialog "Image Format" default answer ¬
"jpg" buttons {"Continue…", "Cancel"} ¬
default button 1)
--repeat
repeat with i in draggedItems
set theFile to (i as alias)
set theFilePath to (the POSIX path of theFile)
set fullFileName to name of (info for theFile without size)
set AppleScript's text item delimiters to "."
set fileNameNoExtension to first text item of fullFileName
--set fileExtension to second text item of fullFileName
set AppleScript's text item delimiters to tid
do shell script ("/usr/local/bin/convert " & quoted form of theFilePath & " -resize " & newWidth & "x" & newHeight & "\\! ~/desktop/" & fileNameNoExtension & "." & newFormat)
end repeat
end open
on open draggedItems is already an repeat loop, if you drop 4 files on this droplet
on open draggedItems
display dialog (draggedItems as text)
end open
you will get 4 different dialogs.
You could do something like this
property askingForNew : true
on open draggedItems
if askingForNew is true then
--display dialogs
set askingForNew to false
end if
display dialog (draggedItems as text)
end open
or drag the folder on the droplet and go through the files of it (like this).
I would like to be able to send the output of a dialog box to my email. What would be the best way to do such a thing?
It would be something sort of like this:
repeat
display dialog "Enter some text:" buttons {"Git Goin"} default answer ""
set theNewInfo to text returned of result
if theNewInfo ≠ "" then exit repeat
end repeat
Its a really simple script for a proof of concept, but what I want is as follows: When they enter any text into the dialog box, for that text to be sent to my email, regardless of what it contains.The Subject would say "NewInfo" and the body would contain the text entered into the dialog box
You should post what code you have... too many questions still to be able to answer you reliably. What are you wanting to send to the email message? email addy? subject? body? etc.
Basically, you capture the result of the dialog and then put it into a "mailto:" URL string and then use 'open location' on the URL. This should be enough to get you started:
set dialogResult to display dialog "Enter the subject" default answer "My subject" buttons {"me#example.com", "you#example.com"} default button 1
set theAddress to button returned of dialogResult
set theSubject to text returned of dialogResult
set theBody to "This%20is%20the%20body%20text:%0AMore%20text"
-- Must encode entities, spaces as %20 and line breaks as %0A (%0D%0A), etc.
set theSubject to findReplace(" ", "%20", theSubject)
set theSubject to findReplace(return, "%0A", theSubject)
set theURL to "mailto:" & theAddress & "?subject=" & theSubject & "&body=" & theBody
open location theURL
on findReplace(f, r, s)
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to f
set s to text items of s
set AppleScript's text item delimiters to r
set s to s as string
set AppleScript's text item delimiters to otid
return s
end findReplace
I've written an Applescript to automate watermarking and resizing images for my company. Everything generally works fine — the script saves the initial history state to a variable, resizes the image, adds the appropriate watermark, saves off a jpeg, then reverts to the initial history state for another resize and watermark loop.
The problem is when I try not to use a watermark and only resize by setting the variable wmColor to "None" or "None for all". It seems that after resizing and saving off a jpeg, Photoshop doesn't like it when I try to revert to the initial history state. This is super annoying, since clearly a resize should count as a history step, and I don't want to rewrite the script to implement multiple open/close operations on the original file. Does anyone know what might be going on? This is the line that's generating the problem (it's in both the doBig and doSmall methods, and throws an error every time I ask it just to do an image resize and change current history state:
set current history state of current document to initialState
and here's the whole script:
property type_list : {"JPEG", "TIFF", "PNGf", "8BPS", "BMPf", "GIFf", "PDF ", "PICT"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png", "psd", "bmp", "gif", "jp2", "pdf", "pict", "pct", "sgi", "tga"}
property typeIDs_list : {"public.jpeg", "public.tiff", "public.png", "com.adobe.photoshop-image", "com.microsoft.bmp", "com.compuserve.gif", "public.jpeg-2000", "com.adobe.pdf", "com.apple.pict", "com.sgi.sgi-image", "com.truevision.tga-image"}
global myFolder
global wmYN
global wmColor
global nameUse
global rootName
global nameCount
property myFolder : ""
-- This droplet processes files dropped onto the applet
on open these_items
-- FILTER THE DRAGGED-ON ITEMS BY CHECKING THEIR PROPERTIES AGAINST THE LISTS ABOVE
set wmColor to null
set nameCount to 0
set nameUse to null
if myFolder is not "" then
set myFolder to choose folder with prompt "Choose where to put your finished images" default location myFolder -- where you're going to store the jpgs
else
set myFolder to choose folder with prompt "Choose where to put your finished images" default location (path to desktop)
end if
repeat with i from 1 to the count of these_items
set totalFiles to count of these_items
set this_item to item i of these_items
set the item_info to info for this_item without size
if folder of the item_info is true then
process_folder(this_item)
else
try
set this_extension to the name extension of item_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of item_info
on error
set this_filetype to ""
end try
try
set this_typeID to the type identifier of item_info
on error
set this_typeID to ""
end try
if (folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
-- THE ITEM IS AN IMAGE FILE AND CAN BE PROCESSED
process_item(this_item)
end if
end if
end repeat
end open
-- this sub-routine processes folders
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
set the item_info to info for this_item without size
if folder of the item_info is true then
process_folder(this_item)
else
try
set this_extension to the name extension of item_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of item_info
on error
set this_filetype to ""
end try
try
set this_typeID to the type identifier of item_info
on error
set this_typeID to ""
end try
if (folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
-- THE ITEM IS AN IMAGE FILE AND CAN BE PROCESSED
process_item(this_item)
end if
end if
end repeat
end process_folder
-- this sub-routine processes files
on process_item(this_item)
set this_image to this_item as text
tell application id "com.adobe.photoshop"
set saveUnits to ruler units of settings
set display dialogs to never
open file this_image
if wmColor is not in {"None for all", "White for all", "Black for all"} then
set wmColor to choose from list {"None", "None for all", "Black", "Black for all", "White", "White for all"} with prompt "What color should the watermark be?" default items "White for all" without multiple selections allowed and empty selection allowed
end if
if wmColor is false then
error number -128
end if
if nameUse is not "Just increment this for all" then
set nameBox to display dialog "What should I call these things?" default answer ("image") with title "Choose the name stem for your images" buttons {"Cancel", "Just increment this for all", "OK"} default button "Just increment this for all"
set nameUse to button returned of nameBox -- this will determine whether or not to increment stem names
set rootName to text returned of nameBox -- this will be the root part of all of your file names
set currentName to rootName
else
set nameCount to nameCount + 1
set currentName to rootName & (nameCount as text)
end if
set thisDocument to current document
set initialState to current history state of thisDocument
set ruler units of settings to pixel units
end tell
DoSmall(thisDocument, currentName, initialState)
DoBig(thisDocument, currentName, initialState)
tell application id "com.adobe.photoshop"
close thisDocument without saving
set ruler units of settings to saveUnits
end tell
end process_item
to DoSmall(thisDocument, currentName, initialState)
tell application id "com.adobe.photoshop"
set initWidth to width of thisDocument
if initWidth < 640 then
resize image thisDocument width 640 resample method bicubic smoother
else if initWidth > 640 then
resize image thisDocument width 640 resample method bicubic sharper
end if
set myHeight to height of thisDocument
set myWidth to width of thisDocument
if wmColor is in {"White", "White for all"} then
set wmFile to (path to resource "water_250_white.png" in bundle path to me) as text
else if wmColor is in {"Black", "Black for all"} then
set wmFile to (path to resource "water_250_black.png" in bundle path to me) as text
end if
if wmColor is not in {"None", "None for all"} then
open file wmFile
set wmDocument to current document
set wmHeight to height of wmDocument
set wmWidth to width of wmDocument
duplicate current layer of wmDocument to thisDocument
close wmDocument without saving
translate current layer of thisDocument delta x (myWidth - wmWidth - 10) delta y (myHeight - wmHeight - 10)
set opacity of current layer of thisDocument to 20
end if
set myPath to (myFolder as text) & (currentName) & "_640"
set myOptions to {class:JPEG save options, embed color profile:false, quality:12}
save thisDocument as JPEG in file myPath with options myOptions appending lowercase extension
set current history state of current document to initialState
end tell
end DoSmall
to DoBig(thisDocument, currentName, initialState)
tell application id "com.adobe.photoshop"
set initWidth to width of thisDocument
if initWidth < 1020 then
resize image thisDocument width 1020 resample method bicubic smoother
else if initWidth > 1020 then
resize image thisDocument width 1020 resample method bicubic sharper
end if
set myHeight to height of thisDocument
set myWidth to width of thisDocument
if wmColor is in {"White", "White for all"} then
set wmFile to (path to resource "water_400_white.png" in bundle path to me) as text
else if wmColor is in {"Black", "Black for all"} then
set wmFile to (path to resource "water_400_black.png" in bundle path to me) as text
end if
if wmColor is not in {"None", "None for all"} then
open file wmFile
set wmDocument to current document
set wmHeight to height of wmDocument
set wmWidth to width of wmDocument
duplicate current layer of wmDocument to thisDocument
close wmDocument without saving
translate current layer of thisDocument delta x (myWidth - wmWidth - 16) delta y (myHeight - wmHeight - 16)
set opacity of current layer of thisDocument to 20
end if
set myPath to (myFolder as text) & (currentName) & "_1020"
set myOptions to {class:JPEG save options, embed color profile:false, quality:12}
save thisDocument as JPEG in file myPath with options myOptions appending lowercase extension
set current history state of current document to initialState
end tell
end DoBig
If you choose a color : save document "Ducky.tif" as JPEG in file ..... the current document will be document "Ducky.tif".
If you choose "None" or "None for all" : save document "Ducky.tif" as JPEG in file ...... the current document will be document "image_640".
So the variable initialState = history state 2 of document "Ducky.tif" give an error, because this document no longer exists.
To leaving original open, here's a solution , use copying true in your save command.
save thisDocument as JPEG in file myPath with options myOptions appending lowercase extension with copying