I'm new to Applescript and the world of coding in general. I'm trying small scripts to get my feet wet and have some fun.
I want to make a random prompt generator. I know how to set the variable to a list but what I have in mind is much bigger than a few choices. I was wondering how to get the information from a text file. In said file I have over 200 prompts to choose from. I want to make a script that selects a random one from among these and displays them in a dialog box. I'm having trouble setting the initial variable to the contents of the file as a list. The follow up (random selection) I think I have a pretty good grasp on. I hope this is clear and thank you for looking.
Edit:
Here's my code so far. I keep running into an error: "Can't make "/Users/Home/Desktop/text.rtf" into type file." Not sure what that means.
set draw_promptList to {}
set draw_prompt to read "/Users/Home/Desktop/text.rtf"
repeat with i from 1 to count of paragraphs in draw_prompt
set end of draw_prompt to paragraph i of draw_prompt
end repeat
set the list_count to the count of draw_prompt
set pick to random number from 1 to list_count
display dialog "Try drawing " & some item of draw_prompt & return
Edit 9/28:I fixed it per the revisions and it works. It does exactly what I wanted: selects a random word from among the long list of words in the text file.
set draw_promptList to {}
set draw_prompt to read "/Users/Home/Desktop/test.txt"
set the list_count to the count of paragraphs in draw_prompt
set pick to random number from 1 to list_count
repeat with i from 1 to count of paragraphs in draw_prompt
set end of draw_promptList to paragraph i of draw_prompt
end repeat
display dialog "Try drawing " & some item of draw_promptList
If I understand correctly what you're asking, reading a file and turn each line of that file into an item in a list, then here is one way it can be done.
set theFileOfPromptsList to {}
set theFileOfPrompts to read "/path/to/FileOfPrompts"
repeat with i from 1 to count of paragraphs in theFileOfPrompts
set end of theFileOfPromptsList to paragraph i of theFileOfPrompts
end repeat
If you want to insure you don't add blank lines, if there are any, to the list, then use the following instead:
set theFileOfPromptsList to {}
set theFileOfPrompts to read "/path/to/FileOfPrompts"
repeat with i from 1 to count of paragraph in theFileOfPrompts
if paragraph i of theFileOfPrompts is not "" then
set end of theFileOfPromptsList to paragraph i of theFileOfPrompts
end if
end repeat
Related
I'd need to split or extract only numbers made of 8 digits from a string in Google Sheets.
I've tried with SPLIT or REGEXREPLACE but I can't find a way to get only the numbers of that length, I only get all the numbers in the string!
For example I'm using
=SPLIT(lower(N2),"qwertyuiopasdfghjklzxcvbnm`-=[]\;' ,./!:##$%^&*()")
but I get all the numbers while I only need 8 digits numbers.
This may be a test value:
00150412632BBHBBLD 12458 32354 1312548896 ACT inv 62345471
I only need to extract "62345471" and nothing else!
Could you please help me out?
Many thanks!
Please use the following formula for a single cell.
Drag it down for more cells.
=INDEX(TRANSPOSE(QUERY(TRANSPOSE(IF(LEN(SPLIT(REGEXREPLACE(A2&" ","\D+"," ")," "))=8,
SPLIT(REGEXREPLACE(A2&" ","\D+"," ")," "),"")),"where Col1 is not null ",0)))
Functions used:
QUERY
INDEX
TRANSPOSE
IF
LEN
SPLIT
REGEXREPLACE
If you only need to do this for one cell (or you have your heart set on dragging the formula down into individual cells), use the following formula:
=REGEXEXTRACT(" "&N2&" ","\s(\d{8})\s")
However, I suspect you want to process the eight-digit number out of all cells running N2:N. If that is the case, clear whatever will be your results column (including any headers) and place the following in the top cell of that otherwise cleared results column:
=ArrayFormula({"Your Header"; IF(N2:N="",,IFERROR(REGEXEXTRACT(" "&N2:N&" ","\s(\d{8})\s")))})
Replace the header text Your Header with whatever you want your actual header text to be. The formula will show that header text and will return all results for all rows where N2:N is not null. Where no eight-digit number is found, null will be returned.
By prepending and appending a space to the N2:N raw strings before processing, spaces before and after string components can be used to determine where only eight digits exist together (as opposed to eight digits within a longer string of digits).
The only assumption here is that there are, in fact, spaces between string components. I did not assume that the eight-digit number will always be in a certain position (e.g., first, last) within the string.
Try this, take a look at Example sheet
=FILTER(TRANSPOSE(SPLIT(B2," ")),LEN(TRANSPOSE(SPLIT(B2," ")))=8)
Or this to get them all.
=JOIN(" ,",FILTER(TRANSPOSE(SPLIT(B2," ")),LEN(TRANSPOSE(SPLIT(B2," ")))=8))
Explanation
SPLIT with the dilimiter set to " " space TRANSPOSE and FILTER TRANSPOSE(SPLIT(B2," ") with the condition1 set to LEN(TRANSPOSE(SPLIT(B2," "))) is = 8
JOIN the outputed column whith " ," to gat all occurrences of number with a length of 8
Note: to get the numbers with the length of N just replace 8 in the FILTER function with a cell refrence.
Using this on a cell worked just fine for me:
(cell_with_data)=REGEXEXTRACT(A1,"[0-9]{8}$")
Now I am working on a file-rename-applescript-project. Here is an example: The.Fantasy.1997.DVDRip.XviD-ETRG.avi.
Now I want to check if the filename contains four digits year number. In this case, it's 1997. The year number MUST begin with 19 or 20 and MUST contain four digits.
If the result is true I will do something, if false I will do something else.
I try to use regex but can't find the solution. It's out of my range. Now I m looking for help here, Thanks a million.
If you want to avoid regex completely, do something like below, using text item delimiters:
(*
This first bit breaks the string up into a list of words by cutting the string
at the period delimiter.
*)
set tid to my text item delimiters
set my text item delimiters to "."
set bits_list to text items of file_name_string
set my text item delimiters to tid
(*
This repeat loop goes though the list of words and tests them (first) to see
if it can be converted to an integer, and (second) whether the number is between
1900 and 2100. If so, it chooses it as the year.
*)
repeat with this_item in bits_list
try
set possibleYear to this_item as integer
if possibleYear ≥ 1900 and possibleYear < 2100 then
-- do what you want with the year value here
exit repeat
end if
end try
end repeat
Of course, this will not work properly if there's a number in the name (e.g., "2001.A.Space.Odyssey.1968.avi") or if a file name has different delimiters (e.g., a space or a dash). But you'd run into those problems using regex as well, so...
Since you're only wishing to check whether or the filename contains a four-digit year within the range 1900-2099, you can do this very simply by defining a handler like so:
on hasYearInTitle(filmTitle as text)
repeat with yyyy from 1900 to 2099
if yyyy is in the filmTitle then return true
end repeat
return false
end hasYearInTitle
Then you can call this handler and pass it a film title, like so:
hasYearInTitle("The.Fantasy.1997.DVDRip.XviD-ETRG.avi") --> true
hasYearInTitle("The.Fantasy.197.DVDRip.XviD-ETRG.avi") --> false
hasYearInTitle("2001.A.Space.Odyssey.1968.avi") --> true
hasYearInTitle("2001.A.Space.Odyssey.avi") --> true (hm...)
As a side-note, films indexed by newznab servers follow a strict file-naming protocol that allow a media server (on your machine) to parse it easily and extract information quickly, pertaining to (as seen in your example file name): the film's title, the film's release date, the source material, the encoding quality, the encoding format (codec), the release group, and the containing file format.
Although some filenames contain more information, and some they should always appear in an set order. This makes them very simple to parse yourself should you need to, but if you're looking to create an organised media library, you would be best looking at using media server, of which there are excellent, freeware, long-standing software options available for macOS and pretty much any other operating system.
The regex .+\.(?:19:20)\d{2}\..+ should do it
The breakdown:
.+ 1 or more any characters
\. An actual dot
(?:19|20) The string "19" or "20" (non-capturing group)
\d{2} Exactly two digits
\. An actual dot
.+ 1 or more any characters
I have a very large text file that I have opened it in VIM and I need to add 1 to numbers matching some criteria. for example:
Comment 1
Comment 2
[p2,0,0,115]Live! ConcertGrand
[p2,2,104,5]Live! PopGrand
[p2,3,104,4]Live! RockPiano
[p2,4,104,3]Live! AmbientPiano
End of file.
and I'd like to transform this (by adding say 1 to the second element of the list) to
Comment 1
Comment 2
[p2,1,0,115]Live! ConcertGrand
[p2,3,104,5]Live! PopGrand
[p2,4,104,4]Live! RockPiano
[p2,5,104,3]Live! AmbientPiano
End of file.
How can I do this in vim, please?
I have tried:
%s/\[p\zs\d\+\(,\d\+\)\=/\=(1+str2nr(submatch(1)))/
But it does not work. Any help would be greatly appreciated. CS
Do you really need to do this with search and replace? Because there is builtin functionality for addition and subtraction.
:h CTRL-A
CTRL-A: Add [count] to the number or alphabetic character at or after the cursor.
{Visual}CTRL-A: Add [count] to the number or alphabetic character in the highlighted text. {not in Vi}
So you basically could use VISUAL-BLOCK selection on your column of numbers and press CTRL-A and it will add 1 to all of them
If it is more complicated you could use macro
I want to add a dash in front of a continuing subtitle line. Like this:
Example sub (.srt):
1
00:00:48,966 --> 00:00:53,720
Today he was so angry and happy
at the same time,
2
00:00:53,929 --> 00:00:57,683
he went to the store and bought a
couple of books. Then the walked home
3
00:00:57,849 --> 00:01:01,102
with joy and jumped in the pool.
4
00:00:57,849 --> 00:01:01,102
One day he was in a bad mood and he
didn't get happier when he read.
TO THIS:
1
00:00:48,966 --> 00:00:53,720
Today he was so angry and happy
at the same time-
2
00:00:53,929 --> 00:00:57,683
-he went to the store and bought a
couple of books. Then the walked home-
3
00:00:57,849 --> 00:01:01,102
-with joy and jumped in the pool.
4
00:00:57,849 --> 00:01:01,102
One day he was in a bad mood and he
didn't get happier when he read.
The original subtitle is in Swedish. This is the standard for scandinavian subtitles.
How do I format it with regex in Notepad++? How should I write the tags and what if the subtitle contains italic tags in front and end?
You can use this regex with the g and m modifiers:
(?:,|([^.?!]<[^>]+>|[^>.?!]))$(\n\n.*\n.*\n)
Use $1-$2- as the substitution.
I'm using a simple definition of sentence. If there is one of .?!, that's counted as the end of a sentence. While this may not be a perfect definition, you're only looking at the ends of sentences.
Depending on several factors (for example, a line ending in ), you may need to tweak it a little.
Essentially, the regex is two parts.
The first part matches one of three things at the end of a line. If it matches a comma, that comma is removed. Otherwise, it looks to see if the last letter (if there is a tag, the letter before that) is NOT any of .?!.
The second part matches all the lines before the one that needs the dash. This also helps ensure that the end of the line you just matched is followed by a new line (and not more text).
I have a big text file that contains a list of items, such as:
a. item number one
b. item number two
c. item number three
d. ..
e. ..
Under that list there is an elaborating text that gives an idea about each item, such as:
At home we need item number one as it helps in cleaning. As for item number two, if you are a handy person you will certainly need it as it protects your hands. In regards to item number three, it is a must to have to protect your children from crawling insects. Etc.
What I am after is a find & replace expression for Notepad++ to select eg "item number one" only without "a. " (a dot space) from the list and replace it with html colour formatting syntax <font color = "red">item number one</font>, so the listed item will be distinguishable in red when the elaborating text is displayed in the browser. Thus, the previous paragraph will be like the following before it is pasted on the website:
At home we need <font color = "red">item number one</font> as it helps in cleaning. As for <font color = "red">item number two</font>, if you are a handy person you will certainly need it as it protects your hands. In regards to <font color = "red">item number three</font>, it is a must to have to protect your children from crawling insects. Etc.
Can anyone help with this please?
Thanks in advance..
How about:
Find what: (item number \w+)
Replace with: <font color = "red">$1</font>
According to your comment, here is a way to select what you want in your list of items:
Find what: (?<=^\w\. ).+$