Loop with records - applescript

Why can't I get this script to show me a list of the records from a specific 'Klasse'?
I keep getting this error:
"error "Can not get Fornavn of {{Klasse:\"1X\", Fornavn:\"Anders\", Efternavn:\"Andersen\", ElevID:\"12345\"}, etc.." (I translated the error from Danish to English)
set varElevnavnIDliste to {{Klasse:"1X", Fornavn:"Anders", Efternavn:"Andersen", ElevID:"12345"}, {Klasse:"1X", Fornavn:"Julius", Efternavn:"Nielsen", ElevID:"23442"}, {Klasse:"1X", Fornavn:"Allan", Efternavn:"Mortensen", ElevID:"32193"}, {Klasse:"1X", Fornavn:"Lukas", Efternavn:"Olsen", ElevID:"87263"}, {Klasse:"1X", Fornavn:"Victor", Efternavn:"Nielsen", ElevID:"34523"}, {Klasse:"3Y", Fornavn:"Kenneth", Efternavn:"Oddersen", ElevID:"23442"}, {Klasse:"3Y", Fornavn:"Thomas", Efternavn:"Johansen", ElevID:"23452"}, {Klasse:"3Y", Fornavn:"Johan", Efternavn:"Thomasen", ElevID:"76590"}, {Klasse:"3Y", Fornavn:"Charlotte", Efternavn:"Frandsen", ElevID:"78569"}, {Klasse:"3Y", Fornavn:"Mathilde", Efternavn:"Charlottesen", ElevID:"64569"}, {Klasse:"STAFF", Fornavn:"Poul", Efternavn:"Killegaard", ElevID:"45328"}, {Klasse:"STAFF", Fornavn:"Frederik", Efternavn:"Augustesen", ElevID:"75639"}, {Klasse:"STAFF", Fornavn:"Cornelius", Efternavn:"Bugesen", ElevID:"75630"}, {Klasse:"STAFF", Fornavn:"Rikke", Efternavn:"Hansen", ElevID:"74632"}, {Klasse:"STAFF", Fornavn:"Katja", Efternavn:"Steffensen", ElevID:"65939"}}
set varResult to {}
repeat with iPar2 from 1 to (number of items in varElevnavnIDliste)
if item 1 of varElevnavnIDliste contains {Klasse:"1X"} then
set end of varResult to Fornavn of varElevnavnIDliste
end if
end repeat
choose from list varResult

Two mistakes:
The iteration element is item iPar2 of varElevnavnIDliste, not item 1 of ...
The reference to the requested element is Fornavn of item iPar2 of varElevnavnIDliste rather than Fornavn of varElevnavnIDliste.
set varResult to {}
repeat with iPar2 from 1 to (number of items in varElevnavnIDliste)
if item iPar2 of varElevnavnIDliste contains {Klasse:"1X"} then
set end of varResult to Fornavn of item iPar2 of varElevnavnIDliste
end if
end repeat
This is the same as
set varResult to {}
repeat with iPar2 from 1 to (number of items in varElevnavnIDliste)
set theItem to item iPar2 of varElevnavnIDliste
if theItem contains {Klasse:"1X"} then
set end of varResult to Fornavn of theItem
end if
end repeat
or still cleaner with the repeat with ... in syntax.
set varResult to {}
repeat with iPar2 in varElevnavnIDliste
if iPar2 contains {Klasse:"1X"} then
set end of varResult to Fornavn of iPar2
end if
end repeat

Related

AppleScript--item of list 1 contain Item of list 2

I want to write an inspection program.
If the item in List 2 is contained in List 1, the next item is checked. If not included, write this item to list 1.
set list_1 to {"万", "历", "阞", "劜", "办", "劝", "功", "加", "劢", "务", "动", "劣", "劤", "劥", "劦", "劧", "助", "劫", "劲", "劳", "励", "努", "劬", "劭", "労", "劰", "劮", "劯", "劾", "劼", "劻", "势", "効", "劷", "劵", "劶", "勆", "劽", "劺", "劸", "勃", "勉", "勋", "勇", "勅", "勁", "勂", "勀", "勊", "勄", "勈", "勐", "勍", "勑", "勌", "勏", "勎", "勒", "勔", "勖", "勚", "動", "勓", "募", "勛", "勞", "勜", "勤", "勣", "勢", "勦", "勠", "勡", "勧", "勨", "勩", "勱", "勥", "勭", "勫", "勪", "勬", "勲", "勯", "勮", "勵", "勳", "勶", "勴", "勷", "勸", "仂", "伤", "艻", "边", "夯", "叻", "另", "屴", "饬", "扐", "抛", "拋", "氻", "忇", "幼", "爋", "朸", "牞", "玏", "朂", "肋", "攰", "男", "穷", "虏", "糼", "竻", "觔", "辦", "赲", "釛", "飭", "鳨", "", "为"}
set list_2 to {"勇", "勫", "陸", "無", "給", "個", "幫", "爸", "姐"}
set numbet_ to 1
repeat
try
repeat
set get_ to get item number_ of list_2
if list_1 contains get_ then
set numbet_ to number_ + 1
exit repeat
else if list_1 does not contain get_ then
set end of words_contained_in_piangpangwubi_and_pianpangcheshi to x4
set numbet_ to number_ + 1
exit repeat
end if
end repeat
on error
exit repeat
end try
end repeat
list_1
to putItems from M into L
if M = {} then return
script
property array : L
property list : M
end script
tell the result
set [x, x_] to [item 1, rest] of its list
if x is not in its array then set end of its array to x
putItems from x_ into its array
end tell
end putItems
Then:
putItems from list_2 into list_1
list_1
Although I don't know if your program has a limit on the number of list items. But if I use such a program, it will not go wrong. You can have a look
set words_contained_in_piangpangwubi to {"万", "历", "阞"}
set words_contained_in_piangpangwubi to {"万", "为", "历"}
repeat with item_number in words_contained_in_piangpangcheshi
set booleanlist to {}
repeat with item_number_2 in words_contained_in_piangpangwubi
if contents of item_number_2 is not contents of item_number then
set end of booleanlist to true
end if
if contents of item_number_2 is contents of item_number then
set end of booleanlist to false
end if
end repeat
set booleanlist_number to 0
repeat with booleanlist_number_2 in booleanlist
if contents of booleanlist_number_2 is true then
set booleanlist_number to booleanlist_number + 1
end if
if contents of booleanlist_number_2 is false then
exit repeat
end if
end repeat
if booleanlist_number = (count item of words_contained_in_piangpangwubi) then
set end of words_contained_in_piangpangwubi to contents of item_number
end if
end repeat
words_contained_in_piangpangwubi

Choose from list of records

How do I from this list of records, get a 'choose from list' showing only 'Fornavn2' and 'Efternavn2', but setting var to corresponding 'ElevID2'???
I know this is not the correct syntax, but it goes to show what I want:
set var to ElevID2 of varEleveriklasse to choose from list (Fornavn2 of varEleveriklasse & " " & Efternavn2 of varEleveriklasse)
List:
set varEleveriklasse to {{Fornavn2:"Kenneth", Efternavn2:"Oddersen", ElevID2:"23442"}, {Fornavn2:"Thomas", Efternavn2:"Johansen", ElevID2:"23452"}, {Fornavn2:"Johan", Efternavn2:"Thomasen", ElevID2:"76590"}, {Fornavn2:"Charlotte", Efternavn2:"Frandsen", ElevID2:"78569"}, {Fornavn2:"Mathilde", Efternavn2:"Charlottesen", ElevID2:"64569"}}
It's impossible to populate the list without a repeat loop.
A smart solution to get the index of the chosen item is to include it in the list.
set varEleveriklasse to {{Fornavn2:"Kenneth", Efternavn2:"Oddersen", ElevID2:"23442"}, ¬
{Fornavn2:"Thomas", Efternavn2:"Johansen", ElevID2:"23452"}, ¬
{Fornavn2:"Johan", Efternavn2:"Thomasen", ElevID2:"76590"}, ¬
{Fornavn2:"Charlotte", Efternavn2:"Frandsen", ElevID2:"78569"}, ¬
{Fornavn2:"Mathilde", Efternavn2:"Charlottesen", ElevID2:"64569"}}
set fullNameList to {}
set indexCounter to 1
repeat with anItem in varEleveriklasse
set end of fullNameList to (indexCounter as text) & space & anItem's Fornavn2 & space & anItem's Efternavn2
set indexCounter to indexCounter + 1
end repeat
set chosenItem to choose from list fullNameList
if chosenItem is false then return
set chosenItem to item 1 of chosenItem // chosenItem is a list or boolean false
set listIndex to word 1 of chosenItem as integer
set ElevID to ElevID2 of item listIndex of varEleveriklasse
display dialog ElevID

How to open a text file and rename it with the text from the file?

I am trying to rename some text files with applescript using a string that is in the text of the file. The files are old usenet messages that ended up with severely messed up names like REARIY-1. I have figured out how to read the file and find the text string I want (it is proceeded by SUBJECT:) I have also figured out how to rename a file if I have the user type in the text, but I have had trouble opening the file to read it and also getting the path to allow me to rename it.
The best I have done so far is the following (which required the user to be careful to select the same file twice):
set foo to choose file with multiple selections allowed
set fileAlias to foo as alias
set footext to read file ((choose file with prompt "phhhht") as string)
set targetText to "Subject:"
set extractedProperty to ""
set foundIt to "Target text not found."
set oldDelims to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to {ASCII character 13}
set bar to text items of footext
repeat with thisItem in bar
if thisItem contains targetText then
set foundIt to thisItem
exit repeat
end if
end repeat
set AppleScript's text item delimiters to oldDelims
on error
set AppleScript's text item delimiters to oldDelims
end try
try
if thisItem contains "not found" then
display dialog "text not found, filename not changed"
tell me to quit
end if
end try
repeat with thisWord in words of foundIt
if contents of thisWord is not targetText then
set extractedProperty to extractedProperty & thisWord & " "
end if
end repeat
set extractedProperty to characters 9 thru -2 of extractedProperty as text
display dialog extractedProperty -- for check only
tell application "Finder"
set name of fileAlias to (extractedProperty & ".txt")
end tell
You just need to change your third line from:
set foo to choose file with multiple selections allowed
set fileAlias to foo as alias
set footext to read file ((choose file with prompt "phhhht") as string)
to
set footext to read file (foo as string)
You could try something like this.
set fileList to choose file with multiple selections allowed
set targetText to "Subject:"
set targetTextCount to count of characters in targetText
set extractedProperty to ""
repeat with thisItem in fileList
set footext to paragraphs of (read file (thisItem as string))
repeat with this_item in footext
if this_item contains targetText then
set extractedProperty to characters from ((offset of targetText in this_item) + targetTextCount) to -1 of this_item as string
--log extractedProperty
tell application "Finder"
set name of (thisItem as alias) to (extractedProperty & ".txt")
end tell
exit repeat
end if
end repeat
end repeat

Using Applescript to export list of URLs based on tags?

I am new to Applescript, so I found an awesome script online that will list all Evernote snaps that have an associated URL . For listing all snaps with URLs, this solution is great. How could I modify this script to filter the listed URLs based on specific tags?
Script I am currently using: http://veritrope.com/code/save-a-list-of-urls-from-your-evernote-items-as-a-file/
tell application "Evernote"
activate
set listOfNotebooks to {}
set EVNotebooks to every notebook
repeat with currentNotebook in EVNotebooks
set currentNotebookName to (the name of currentNotebook)
copy currentNotebookName to the end of listOfNotebooks
end repeat
set Folders_sorted to my simple_sort(listOfNotebooks)
set SelNotebook to choose from list of Folders_sorted with title "Select Evernote Notebook" with prompt ¬
"Current Evernote Notebooks" OK button name "OK"
set EVnotebook to item 1 of SelNotebook
set listofNotes to {}
set note_Records to {}
set allNotes to every note in notebook EVnotebook
repeat with currentNote in allNotes
try
set currentNoteURL to (the source URL of currentNote)
set currentNoteTitle to title of currentNote
if currentNoteURL is not missing value then
copy currentNoteURL to the end of listofNotes
copy {name:currentNoteTitle, URL:currentNoteURL} to the end of note_Records
end if
end try
end repeat
set Notes_sorted to my simple_sort(listofNotes)
set SelNote to ¬
choose from list of Notes_sorted with title ¬
"List Of URLs In Notes" OK button name "Export List" cancel button name "Close Window" with empty selection allowed
set record_Text to {}
repeat with note_Record in note_Records
set theCurrentRecord to ("Title: " & name of note_Record & return & "URL: " & URL of note_Record & return & return) as text
copy theCurrentRecord to the end of record_Text
end repeat
if (SelNote is not false) then
tell application "System Events"
-- convert list to text FILE
set ExportList to "Current List of URLs in Notes for " & EVnotebook & "-- " & (current date) & return & return & record_Text as Unicode text
set fn to choose file name with prompt "Name this file" default name "URL List for Notebook Named " & EVnotebook & ¬
".txt" default location (path to desktop folder)
set fid to open for access fn with write permission
write ExportList to fid
close access fid
end tell
else
set EVnotebook to item 1 of SelNotebook
end if
end tell
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort
Apologies for my code block being funky. If any mods can fix it I'd appreciate the help.
In your repeat with currentNote in allNotes loop you could add something like this:
set allTags to tags of currentNote
repeat with currentTag in allTags
if name of currentTag is "your_specific_tag" then
do what you want
...........
end if
end repeat

Applescript to change rating of song currently playing in iTunes

I set up an applescript to change the rating of the current song in iTunes and it worked the first few times but now does nothing. Here is the code:
tell application "System Events"
if (name of processes) contains "iTunes" then
set iTunesRunning to true
else
set iTunesRunning to false
end if
end tell
if iTunesRunning then
tell application "iTunes"
repeat with theTrack in (get selection)
set the rating of theTrack to 100
end repeat
end tell
end if
Anyone see any glaring issues in there? FYI, this is attached to a global hotkey, but even opening it in Applescript Editor and hitting "Run" does nothing.
Ack. All I had to was ask to find the answer myself.
tell application "System Events"
if (name of processes) contains "iTunes" then
set iTunesRunning to true
else
set iTunesRunning to false
end if
end tell
if iTunesRunning then
tell application "iTunes"
set rating of current track to 100
end tell
end if
I don't remember where I found this, but it's a more robust version that might help you. I execute it with a key command using FastScripts. It brings up a dialog box prompting you to rate the current song on a scale of 0 to 5 with a default rating of 4.
tell application "Finder"
set frontApp to name of first process whose frontmost is true
end tell
set currTrack to ""
set thisNum to false
tell application "iTunes"
if player state = playing then
set currTrack to current track
set thisName to name of current track
set thisArt to artist of current track
set numList to {"0", "1", "2", "3", "4", "5", "", "Rate All"}
set thisRating to rating of currTrack
set songArt to ("'" & thisName & "' by " & thisArt)
end if
end tell
tell application frontApp
if currTrack = "" then
beep
tell application frontApp
display dialog "There is no song playing in iTunes at this time." buttons "OOPS" default button "OOPS" with icon note
end tell
else
if thisRating ≠ 0 then
set thisRating to 4
set thisList to (choose from list numList with prompt "Select a rating for this song:" & return & songArt & return & "Select \"Rate All\" to apply rating to all matching songs." default items thisRating OK button name "Select" with empty selection allowed and multiple selections allowed)
else
set thisList to (choose from list numList with prompt "Select a rating for this song:" & return & songArt & return & "Select \"Rate All\" to apply rating to all matching songs." OK button name "Select" with empty selection allowed and multiple selections allowed)
end if
end if
end tell
if thisList ≠ false then
--tell application "iTunes"
set thisRating to ""
repeat with i from 1 to count of items of thisList
set thisItem to item i of thisList
if thisItem = "Rate All" then
if thisRating = "" then
display dialog "You must select a rating to apply to this song." buttons "OK" default button "OK" with icon caution
else
tell application "iTunes"
set dataList to {name, artist, album, time} of currTrack
set addSourceData to {time, size, year, bit rate} of currTrack
set matchList to {"Time", "Size", "Year", "Bit Rate"}
set matchDef to (choose from list matchList with prompt "You may choose criteria to match in addition to track name, artist, and album." with multiple selections allowed and empty selection allowed)
if matchDef = {} then set matchDef to false
set trackList to (every track of library playlist 1 whose ((name = item 1 of dataList) and (artist = item 2 of dataList) and (album = item 3 of dataList)))
set trackCount to count of items of trackList
repeat with j from 1 to trackCount
set thisTrack to item j of trackList
set notMatch to false
if matchDef ≠ false then
set addSynchData to {time, size, year, bit rate} of thisTrack
repeat with m from 1 to 4
if ((m = 1) and (matchDef contains "Time")) or ((m = 2) and (matchDef contains "Size")) or ((m = 3) and (matchDef contains "Year")) or ((m = 4) and (matchDef contains "Bitrate")) then
if item m of addSourceData ≠ item m of addSynchData then
set notMatch to true
exit repeat
end if
end if
end repeat
end if
if notMatch = false then
set rating of thisTrack to thisRating
end if
end repeat
if thisRating > 0 then
set thisRating to thisRating / 20 as integer
end if
end tell
display dialog "Rating of " & thisRating & " applied to " & trackCount & " song(s)." buttons "OK" default button "OK" giving up after 3
end if
else
set thisRating to thisItem * 20
tell application "iTunes"
set rating of currTrack to thisRating
end tell
end if
end repeat
--end tell
end if

Resources