Like this (not tested):
public static void CastByPlayerName(int spellId, string playerName)
{
string lua = "CastSpellByID(spellID, 'focus')";
var target = ObjectManager.GetObjectWoWPlayer().FirstOrDefault(p => p.Name == playerName);
try
{
if (target != null && !target.IsValid)
return;
if (target.IsMyTarget)
{
Lua.LuaDoString(lua.Replace("spellID", spellId.ToString()).Replace("'focus'", "'target'"));
}
else if (target.Guid == ObjectManager.Me.FocusGuid)
{
Lua.LuaDoString(lua.Replace("spellID", spellId.ToString()));
}
else
{
var currentFocus = ObjectManager.Me.FocusGuid;
ObjectManager.Me.FocusGuid = target.Guid;
Lua.LuaDoString(lua.Replace("spellID", spellId.ToString()));
ObjectManager.Me.FocusGuid = currentFocus;
}
}
catch { }
}
In second argument of castspellby... (in legion version), it is not player name, but it is unitid http://wow.gamepedia.com/UnitId