How to open a folder containing a specific string? (Mac) - macos

We got some folders with project IDs with the following Pattern: x123_projectname.
I use Alfred in my workflow and I need to find and open a specific folder by the ID.
It is possible to run applescripts from Alfred. I'm new to applescript and google helped me to create this this:
set theString to "/path/to/folder/containing/projects/"
display dialog "What is the ID?" default answer ""
set theID to the text returned of
(display dialog "What is the ID?" default answer "")
tell application "Finder"
activate
set x to theString & "theID" as alias
open x
end tell
but it didn't work - do you have any hints for me?

You have a couple problems with your script but in general you have the right idea. The main problem is that applescript doesn't work with slash delimited paths. Applescript paths are colon delimited. You can convert from slash to colon delimited using the "posix file" command. Give this a try. Good luck.
set theString to "/path/to/folder/containing/projects/"
display dialog "What is the ID?" default answer ""
set theID to the text returned of result
set macString to POSIX file theString
tell application "Finder"
activate
set theResult to every folder of macString whose name contains theID
open theResult
end tell

Related

Directing an app to run off of a different script in AppleScript.

I'm making an applescript to help with school. You type the subject you wish to access, and then it opens that folder (inside the app). However, some subjects have ebooks, so I want there to be another dialog box asking you wether you want to open the ebook or the folder. Unfortunately, a display dialog can't branch off of another display dialog.
So far, the only way I've found around this is to direct the app to run off of another script (in the "Scripts" folder of the app). I've tried
tell application "Script Editor" to run script (path to me as sting) & "Contents:Resources:Scripts:Subject.scpt"`
But it didn't work. Sorry if I'm barking up the wrong tree and seem completely stupid. Thanks
As far as I understood what you're trying to achieve, the following script would work.
set listOfSubjects to {"Math", "Biology", "History", "Languages"}
set subjectsWithEbook to {"Biology", "History"}
choose from list listOfSubjects with prompt "Select the subject"
set subject to item 1 of result
set openEbook to false
repeat with subjectItem in subjectsWithEbook
if subjectItem contains subject then
choose from list {"Open folder", "Open Ebook"} with prompt "What to open?"
set openType to item 1 of result
if openType contains "Open Ebook" then
set openEbook to true
end if
exit repeat
end if
end repeat
if openEbook then
--Open ebook
else
--Open folder
end if
I am guessing that you want to type the subject name? Here is an example that uses dialog boxes.
set subjectFolderPath to (path to me as string) & "Contents:Resources:Subjects:"
set subjectsWithEbook to {"English", "Spanish"}
display dialog "What subject would you like to access?" default answer ""
set theSubject to text returned of the result
if theSubject is in subjectsWithEbook then
display dialog "Would you like to open the Folder or the Book?" buttons {"Folder", "Book"} default button 1
set toOpen to button returned of the result
if toOpen is "Book" then
-- tell app "Preview" to open ???
beep
else
tell application "Finder" to open folder (subjectFolderPath & theSubject & ":")
end if
else
tell application "Finder" to open folder (subjectFolderPath & theSubject & ":")
end if
You might also want to have a list of subjects that are allowed and error check for them.

Change the color of text by using applescript

I am writing some text in to word file i want to change the color of that text any one can help on that one plz.
I want to print the 'message' from following script in red color.
Here is the Script:
set message to "mostly these windows are popup in application"
on ResultCreationFuction(message)
try
set text_to_save to message as text
tell application "System Events"
tell application "Finder"
set sortedList to sort (get files of folder "SofTestAutomationResult" of desktop) by modification date
set FileCount to get count of sortedList
set theFile to (item FileCount of sortedList) as alias
end tell
set file_ref to open for access theFile with write permission
write (text_to_save & return) to the file_ref starting at eof
close access file_ref
delay 2
end tell
end try
end ResultCreationFuction
Some Details:
The file is word which is all ready present on above location having name "10.012.2014_17_4_20.doc" (the name of .doc file is not fix)
What you are attempting is the wrong way to do it.
To manipulate content like that, including formatted text (not plain
text), you need to work within, ideally, a well-scriptable app, like
Pages (or Word, perhaps, but I don't have that on the machine I'm
writing this from).
Don't use System Events if you don't need to. Use the apps with the appropriate AppleEvents/dictionary, etc. If you don't know what I'm talking about, you need to take advantage of the infinite resource known as the web.
"Fuction" is just bad form.
I would suggest doing a lot more reading up on how AppleScript works (or scripting in general), but to start you out, here is a script I just wrote in pages which sets the color of a specific word of the open document after putting text in there:
tell application "Pages"
set body text of document 1 to "hello there mister fancy pants"
set color of word 3 of body text of page 1 of document 1 to {64614, 0, 111}
end tell
If you have Pages, try this by starting with a blank page and running this script. Obviously, you could get rid of "word 3 of" in the 2nd line, and the whole body text will be red.
I hope this makes sense and is of help.
[edit]
I should mention that even TextEdit is scriptable and can open Word documents. Here's an example using TextEdit:
tell application "TextEdit"
set text of document 1 to "hello mister fancy pants"
set color of words 2 thru 3 of text of document 1 to {65535, 0, 0}
end tell
There is a little danger of non-Word apps losing formatting of Word files. But it just seems you are attempting something very simple, and I'm not sure if Word is really necessary here.
You can't add color using the write to eof. You should open the document in Word and then insert the line and add the color. Here's a script that should demonstrate how:
set text_to_add to "mostly these windows are popup in application"
set theFile to ((path to desktop folder) & "10.012.2014_17_4_20.doc") as string
tell application "Microsoft Word"
set theFile to theFile as string -- assuming theFile is an alias or :: path
open file theFile
tell active document
set endOfDoc to end of content of text object -- insert the text to end of document
set theRange to create range start (endOfDoc - 1) end endOfDoc
insert text text_to_add at theRange
set myRange to create range start endOfDoc end (endOfDoc + (length of text_to_add))
set color index of font object of myRange to red
save
end tell
end tell

How to search itunes store for app with applescript?

How to search itunes store for app with applescript? I have been stuck on this and recording doesn't work in itunes.
I haven’t seen recording work in years. In this case, though, I’m not sure it could. While iTunes is scriptable, it doesn’t appear to have any direct means to perform a search of the Stores.
However, there is a means to perform a search using a URL and “open location”. It would look something like:
tell application "iTunes"
open location "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&country=US&media=software&term=ia%20Writer"
end tell
Put the text you want to search for after &term=; here, that’s ia%20Writer (with the %20 meaning a space).
This combines info from Ars Technica and Doug’s AppleScripts.
Here's a way you can use an AppleScript display dialog to enter what App to search for in the iTunes Store using the answer provided by Jerry Stratton:
If the search has spaces in it, the script replaces it with %20 for you.
set theAppSearch to ""
set theAppSearch to text returned of (display dialog "App Search iTunes Store for:" default answer "" buttons {"Cancel", "OK"} default button 2)
if theAppSearch is not "" then
set theAppSearch to my replaceSpaces(theAppSearch)
tell application "iTunes"
activate
open location "itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?WOURLEncoding=ISO8859_1&lang=1&output=lm&country=US&media=software&term=" & theAppSearch
end tell
end if
on replaceSpaces(theString)
set AppleScript's text item delimiters to {" "}
set theString to text items of theString
set AppleScript's text item delimiters to {"%20"}
set theString to theString as text
set AppleScript's text item delimiters to {""}
return theString
end replaceSpaces

Replacing words in Applescript

I'm back to writing Applescripts. I'm making a script that will Google things for you. Basically, I need to replace all of the spaces with %20. I know a little about text item delimiters, but I don't know how to implement them in this case.
Here's what I got so far:
if userInput contains "Google " then set {TID, text item delimiters} to {text item delimiters, {"Google "}}
if length of userInput is greater than or equal to 2 then set resultString to text item 2 of userInput
if userInput contains "Google " then set text item delimiters to TID
set openPage to (resultString as string)
if userInput contains "Google " then do shell script "open http://www.google.com/search?q=" & openPage
FYI, the userInput variable is the variable I use when dealing with textboxes.
Thanks
I think this script demonstrates the principle of what you’re trying to do:
tell application "Safari"
activate
display dialog "Search Google for:" default answer "" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 60
if the button returned of the result is equal to "OK" then
set theSearch to the text returned of the result
set AppleScript's text item delimiters to space
set theSearchList to every text item of theSearch
set AppleScript's text item delimiters to "+"
set theQuery to theSearchList as text
set AppleScript's text item delimiters to ""
set theLink to "http://www.google.com/search?hl=en&q=" & theQuery & "&btnG=Google+Search"
open location theLink
end if
end tell
However, it’s a lot easier if you just ask an app like BBEdit (or its free version, TextWrangler, which has the same AppleScript dictionary) to do the find and replace for you, because BBEdit is an expert at finding and replacing text, whereas AppleScript by itself is not. AppleScript is a “little language” — it deliberately lacks 95% of the functionality of most programming languages because the functionality is in the apps that you command with AppleScript. In other words, AppleScript expects you to have a text editor with which to process text, therefore AppleScript deliberately doesn’t have the built-in text processing ability of a language like Perl (which you can also use in your AppleScripts.)
So the above script gets a lot shorter, easier to write, easier to read, and easier to understand if you add BBEdit (or the free TextWrangler) into the mix:
tell application "Safari"
activate
display dialog "Search Google for:" default answer "" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 60
if the button returned of the result is equal to "OK" then
set theSearch to the text returned of the result
tell application "BBEdit"
set theQuery to replace " " using "+" searchingString theSearch
end tell
set theLink to "http://www.google.com/search?hl=en&q=" & theQuery & "&btnG=Google+Search"
open location theLink
end if
end tell
Also, notice that you don’t have to run a shell script to open a link in a browser. So this line in your script:
if userInput contains "Google " then do shell script "open http://www.google.com/search?q=" & openPage
… can be written like this:
if userInput contains "Google " then open location "http://www.google.com/search?q=" & openPage
… or like this:
if userInput contains "Google " then
open location "http://www.google.com/search?q=" & openPage
end if
… and you can use the “open location” command with any app, not just Safari. If you tell Finder to “open location” the link will open in your default browser, which might be Chrome.

Get message in compose window from Mail.app

I am trying to make a script that will get the contents of an email message that I'm composing in Mail, do something with the data, and then send the message. I know how to make and send a new message from scratch with AppleScript, but I can't find a way to get a message that I'm already writing. I don't care what language is used, and I would be open to trying a different email client. Thanks for your help!
Mail has huge limitations with regards to Applescript and dealing with its content area is a major one. The best bet is to use GUI scripting to tab to the content area, type cmd-C, and then work off the data in the clipboard.
Sadly from what I can see Applescript has not been improved at all in Lion.
It's actually pretty easy to do what you need.
If you want to run some kind of an inline processing (assigned to, say, a hotkey (in Mail, e.g. Cmd+D is not occupied), or "just" listed in the Services menu, accessible after selecting something), you can simply use Automator. A demo Automator script reading the current selection, making some changes (here, converting some ASCII char+number combinations to some accented characters) and, finally, returning the modified text is as follows:
on run {input, parameters}
set myText to replaceText("a1", "á", (input as text))
set myText to replaceText("e1", "é", myText)
set myText to replaceText("i1", "í", myText)
return myText
end run
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
Make sure you enable the "Replaces selected text", should you want to overwrite the original content with the returned one.
if you want to write an external script not invoked from the local Services menu (or via a hotkey), you'll also need to add clipboard handling. A solution similar to the above with additional clipboard copy/paste:
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
tell application "Mail"
activate
tell application "System Events"
tell process "Mail"
click menu item "Select All" of menu "Edit" of menu bar 1
click menu item "Copy" of menu "Edit" of menu bar 1
end tell
end tell
end tell
tell application "Mail"
set textclip to (the clipboard)
end tell
set myText to replaceText("a1", "á", textclip)
set myText to replaceText("e1", "é", myText)
set myText to replaceText("i1", "í", myText)
set the clipboard to myText
tell application "Mail"
activate
tell application "System Events"
tell process "Mail"
click menu item "Paste" of menu "Edit" of menu bar 1
end tell
end tell
end tell
Note that the latter script selects (and, then, overwrites) the entire window contents. It should be easy to work on the current selection only.
It's possible, but painful. Painful enough that I'm still trying to work out exactly how to do something similar but in Safari. I've gotten to the point where I can find the textarea, but the documentation I've found for getting the content isn't working. (Unfortunately, that's pretty much par for the course for AppleScript; every program does stuff just a little bit differently from the next program.)
EDIT: ok, have some horrible evil which hopefully can be adapted to work with Mail: http://www.ece.cmu.edu/~allbery/edit_textarea.script
This is striaghtforward if we make two reasonably weak assumptions: that the message you're working on is frontmost, and that the subject of all draft messages is unique. Then, before running the script, save the message you're working on; this will place it in the drafts mailbox. Then, since the subject of the message is the name of the window, we can easily access it; and since we can easily access the drafts mailbox, we can combine the two. This gives us:
tell application "Mail"
set msgs to messages of drafts mailbox ¬
whose subject is (name of window 1 as string)
if (count of msgs) = 1 then
-- Do whatever
else
-- Error, disambiguate, whatever
end if
end tell
It's probably possible to make the script save the frontmost window, and it wouldn't surprise me if a freshly-saved message is always the first item of the drafts mailbox, but these are left as an exercise for the reader :-)
So I came across this in 2020 and with this Apple Script it is (now?) possible (whenever its still a bit hacky since I have to use the clipboard for this):
activate application "Mail"
tell application "System Events"
tell process "Mail"
set initialClipboardContent to (the clipboard as text)
set composeWindow to (first window whose title does not contain "Inbox")
set value of attribute "AXFocused" of UI element 1 of scroll area 1 of composeWindow to true
delay 0.05
# CMD + A
key code 0 using command down
delay 0.1
# CMD + C
key code 8 using command down
delay 0.1
set message to (the clipboard as text) as string
log message
set the clipboard to initialClipboardContent
end tell
end tell
Here a proof of concept:

Resources