Applescript to remove all text not between two strings

邮差的信 提交于 2019-12-08 11:48:47

问题


I'm trying to make a script that displays a dialog of the current iTunes Top 20. How I intend to do this is get the html code from the top 100 website and then extract for text between two strings to get the name of the song. For the first song, this is extremely successful.

However, it only works for the first song each time. The only way I can think of to fix this is rather then get everything between the two strings, I could delete everything not between them. This would hopefully then give me all the song names as a string.

Does anyone know how to do this?

Get the HTML:

set curlcommand to "curl https://www.apple.com/itunes/charts/songs/"
set html to (do shell script curlcommand) 

Get the song name:

set AppleScript's text item delimiters to "width=\"100\" height=\"100\" alt=\""
set theText to item 2 of every text item of html
set AppleScript's text item delimiters to "\"></a>"
set theText to item 1 of every text item of theText
set AppleScript's text item delimiters to ""

回答1:


You could use the shell to get a list with all song names line by line (this is not a AppleScript list).

set charts to (do shell script "
curl -s 'https://www.apple.com/itunes/charts/songs/'| tr '\"' '\n' |awk '/^ alt=$/ {getline;print}'")

tr '\"' '\n' substitutes the double quotes with new lines (tr manual page)

awk '/^ alt=$/ {getline;print}' prints the line after the line alt= where the song name is written (awk manual page)



来源:https://stackoverflow.com/questions/46170073/applescript-to-remove-all-text-not-between-two-strings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!