Thursday, October 6, 2022

#1 2022-05-28 02:01:36 am

WallofSound
Member
Registered: 2021-10-30
Posts: 8

Replacing text within tell tags

Hello MacScripter.

I've got a script that opens a program and find and copy some information.
I would then like to replace some of the information within the tell tags.
Is this possible

When I move the "set the MidiNoteNames to replace_chars(MidiNoteNames, "C-2", "00")
etc..." before the end tell tag I get an error message

I've tried to minimize the code to only the necessary bits.
Hope that's ok.


Applescript:

on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

tell application "Finder"
tell application "System Events"
set PT to the first application process whose creator type is "PTul"
tell PT
activate
set frontmost to true


set myList to "C2
D2
F3
A6
F#7"




end tell
end tell
end tell



#These are all the different Midi note names that my program outputs
set the MidiNoteNames to myList as string

#Here I replace the name with a number that I can use in other programs
set the MidiNoteNames to replace_chars(MidiNoteNames, "C-2", "00")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#-2", "01")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D-2", "02")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb-2", "03")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E-2", "04")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F-2", "05")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#-2", "06")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G-2", "07")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#-2", "08")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A-2", "09")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb-2", "10")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B-2", "11")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C-1", "12")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#-1", "13")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D-1", "14")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb-1", "15")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E-1", "16")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F-1", "17")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#-1", "18")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G-1", "19")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#-1", "20")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A-1", "21")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb-1", "22")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B-1", "23")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C0", "24")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#0", "25")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D0", "26")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb0", "27")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E0", "28")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F0", "29")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#0", "30")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G0", "31")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#0", "32")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A0", "33")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb0", "34")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B0", "35")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C1", "36")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#1", "37")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D1", "38")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb1", "39")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E1", "40")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F1", "41")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#1", "42")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G1", "43")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#1", "44")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A1", "45")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb1", "46")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B1", "47")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C2", "48")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#2", "49")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D2", "50")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb2", "51")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E2", "52")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F2", "53")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#2", "54")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G2", "55")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#2", "56")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A2", "57")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb2", "58")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B2", "59")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C3", "60")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#3", "61")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D3", "62")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb3", "63")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E3", "64")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F3", "65")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#3", "66")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G3", "67")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#3", "68")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A3", "69")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb3", "70")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B3", "71")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C4", "72")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#4", "73")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D4", "74")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb4", "75")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E4", "76")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F4", "77")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#4", "78")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G4", "79")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#4", "80")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A4", "81")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb4", "82")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B4", "83")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C5", "84")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#5", "85")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D5", "86")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb5", "87")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E5", "88")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F5", "89")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#5", "90")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G5", "91")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#5", "92")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A5", "93")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb5", "94")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B5", "95")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C6", "96")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#6", "97")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D6", "98")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb6", "99")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E6", "100")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F6", "101")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#6", "102")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G6", "103")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#6", "104")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A6", "105")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb6", "106")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B6", "107")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C7", "108")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#7", "109")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D7", "110")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb7", "111")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E7", "112")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F7", "113")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#7", "114")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G7", "115")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G#7", "116")
set the MidiNoteNames to replace_chars(MidiNoteNames, "A7", "117")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Bb7", "118")
set the MidiNoteNames to replace_chars(MidiNoteNames, "B7", "119")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C8", "120")
set the MidiNoteNames to replace_chars(MidiNoteNames, "C#8", "121")
set the MidiNoteNames to replace_chars(MidiNoteNames, "D8", "122")
set the MidiNoteNames to replace_chars(MidiNoteNames, "Eb8", "123")
set the MidiNoteNames to replace_chars(MidiNoteNames, "E8", "124")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F8", "125")
set the MidiNoteNames to replace_chars(MidiNoteNames, "F#8", "126")
set the MidiNoteNames to replace_chars(MidiNoteNames, "G8", "127")

MidiNoteNames

Kind Regards,


Anders Wall

Last edited by WallofSound (2022-05-28 04:19:28 am)

Offline

 

#2 2022-05-28 04:26:59 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2535

Re: Replacing text within tell tags

Add the keyword my or of me when you call the handler inside the tell block:

set the MidiNoteNames to my replace_chars(MidiNoteNames, "C-2", "00")
or,
set the MidiNoteNames to replace_chars(MidiNoteNames, "C-2", "00") of me


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2022-05-28 06:11:00 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5570

Re: Replacing text within tell tags

WallofSound wrote:

Applescript:

tell application "Finder"
tell application "System Events"
set PT to the first application process whose creator type is "PTul"
tell PT
activate
set frontmost to true


set myList to "C2
D2
F3
A6
F#7"




end tell
end tell
end tell


Hi.

You shouldn't nest 'tell' statements to different applications if it can be avoided, especially here where both the Finder and System Events possess application process elements. The Finder's not needed in the code you've posted anyway, so you only need System Events.

PT, if found, will an application process of either the Finder or System Events, not an application in its own right, so you won't be able to tell it to activate. That section of code would be better written like this:

Applescript:

tell application "System Events"
   set PT to the first application process whose creator type is "PTul"
   set PT's frontmost to true
end tell

set myList to "C2
D2
F3
A6
F#7"


NG

Offline

 

#4 2022-05-29 01:36:29 am

WallofSound
Member
Registered: 2021-10-30
Posts: 8

Re: Replacing text within tell tags

KniazidisR wrote:

Add the keyword my or of me when you call the handler inside the tell block:

set the MidiNoteNames to my replace_chars(MidiNoteNames, "C-2", "00")
or,
set the MidiNoteNames to replace_chars(MidiNoteNames, "C-2", "00") of me


Thank you KniazidisR
I’ll update my script asap and have a go at it.
Cheers,
/Anders

Offline

 

#5 2022-05-29 01:39:09 am

WallofSound
Member
Registered: 2021-10-30
Posts: 8

Re: Replacing text within tell tags

Nigel Garvey wrote:
WallofSound wrote:

Applescript:

tell application "Finder"
tell application "System Events"
set PT to the first application process whose creator type is "PTul"
tell PT
activate
set frontmost to true


set myList to "C2
D2
F3
A6
F#7"




end tell
end tell
end tell


Hi.

You shouldn't nest 'tell' statements to different applications if it can be avoided, especially here where both the Finder and System Events possess application process elements. The Finder's not needed in the code you've posted anyway, so you only need System Events.

PT, if found, will an application process of either the Finder or System Events, not an application in its own right, so you won't be able to tell it to activate. That section of code would be better written like this:

Applescript:

tell application "System Events"
   set PT to the first application process whose creator type is "PTul"
   set PT's frontmost to true
end tell

set myList to "C2
D2
F3
A6
F#7"


Hello Nigel and thank you for your message.
You are of course right.
I have to admit it’s a “copy paste” bit that I for some reason always add to my scripts…
Foolish I know.
Will update them.
Again, thank you!
/Anders

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)